Solved

How to limit insert keyword

Posted on 2013-01-01
4
375 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:Rick Hobbs
ID: 38734300
What is max_input_vars in php.ini set to?
0
 
LVL 31

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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to dynamically set the form action using jQuery.

920 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

16 Experts available now in Live!

Get 1:1 Help Now