Solved

How to limit insert keyword

Posted on 2013-01-01
4
374 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
  • 2
4 Comments
 
LVL 22

Expert Comment

by:rickhobbs
ID: 38734300
What is max_input_vars in php.ini set to?
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 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 30

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now