MDanz Posted August 31, 2010 Share Posted August 31, 2010 how do i limit this textfield to three strings? <input type='text' size='40' name='search' style='font-size:16px; font-family:Arial;font-weight:bold;' /> is this php or javascript? i want only three strings to be allowed entered. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 31, 2010 Share Posted August 31, 2010 Can you clarify what you mean by "three strings"? Quote Link to comment Share on other sites More sharing options...
MDanz Posted August 31, 2010 Author Share Posted August 31, 2010 three words.. Quote Link to comment Share on other sites More sharing options...
Onloac Posted September 1, 2010 Share Posted September 1, 2010 Well I'm sure there is a better way of doing it but could you just count white spaces and have everything after the 4th white space removed? Quote Link to comment Share on other sites More sharing options...
Onloac Posted September 1, 2010 Share Posted September 1, 2010 Alright I just did some searching on google and found what your looking for. This does something similar to what you want. I'm sure you could edit it to your liking. Step 1 of 2 <?php function drupalicious_summarise($paragraph, $limit) { $textfield = strtok($paragraph, " "); while($textfield) { $text .= " $textfield"; $words++; if(($words >= $limit) && ((substr($textfield, -1) == "!")||(substr($textfield, -1) == "."))) break; $textfield = strtok(" "); } return ltrim($text); } ?> Step 2 of 2 <?php print drupalicious_summarise($textfieldname,20);?> $textfieldname = the name of the field you want trimmed. 20 = the number of words, to the nearest sentance, your text will be trimmed to. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2010 Share Posted September 1, 2010 I think I'd use explode, array_chunk, and implode for this one (whether it's the best way or not, I'm not sure). I briefly tested this, and it seems to so what you're after. <?php $string = 'Giant bunch of words and stuff'; $array = array_chunk(explode( ' ', $string), 3); $new_string = implode( ' ', $array[0] ); echo $new_string; // Echos "Giant bunch of" ?> EDIT: If the original string is coming from a database query, you might be able to do this more efficiently using MySQL's built in SUBSTRING_INDEX() function. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.