Jump to content

limit textfield to three strings


MDanz

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.