Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to limit insert keyword

Posted on 2013-01-01
4
Medium Priority
?
382 Views
Last Modified: 2013-01-01
I have a form as below:

<input type="text" id="keywords" name="keywords" class="text" size="20" />

Open in new window


I want when user input keywords separated by a comma as shown below:

nature, index, luxury, test1, test2, ..... test60

Open in new window


for example the user put 60 keywords, But I only get a maximum of 50 key words, how to?
0
Comment
Question by:TamHinh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Rick Hobbs
ID: 38734300
What is max_input_vars in php.ini set to?
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 38734334
I'm not sure to understand, so let me summarize:
you have an input type='text' where a user can put an unlimited number of keywords: you want to grab these keywords only up to 50 and have them separated by a comme. If this is the case, you can try this:

<?php

if (isset($_POST['submit'])){
  $kw = $_POST['keywords'];
  $kw = preg_replace("/\s+/", " ", $kw);
  $kwArr = explode(' ', $kw);
  $output = array_slice($kwArr, 5, count($kwArr));
  $output = array_diff($kwArr, $output);
  $keywords = implode(', ', $output);
  echo $keywords;
}

echo "<form method='post'>";
echo "<input type='text' name='keywords' value='' />";
echo "<input type='submit' name='submit' value='Submit' />";
echo "</form>";

Open in new window


Cheers
0
 

Author Comment

by:TamHinh
ID: 38734349
If I want to limit the maximum number of characters of a keyword, how to? for example, each input keywords only have a maximum of 20 characters
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 38734495
Thanks for points. To limit the length of accepted keywords you can use this:

if (isset($_POST['submit'])){
  $kw = $_POST['keywords'];
  $kw = preg_replace("/\s+/", " ", $kw);
  $kwArr = explode(' ', $kw);
  $output = array_slice($kwArr, 5, count($kwArr));
  $output = array_diff($kwArr, $output);
  for($i = 0; $i<count($output); $i++){
    if (strlen($output[$i]) <= 20){
      $result[] = $output[$i];
    }
  }
  $keywords = implode(', ', $result);
  echo $keywords;
}

Open in new window

Cheers
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question