I wasn't sure how to do this. I want to count the words and make sure the length of the word doesn't go pass a set length.

$textarea = "information from textarea form";

$textarea2 = str_word_count($textarea); //Count words

if (a word in $textarea2 has less than 10 characters) {
do this
}
else {
the word was too long
}

    I'm confused: first you are discussing the counting of words, and then you are discussing the number of characters in a single word. 😕

      You probably need to to use the $format option.
      The 2 value looks useful, it returns the position of the word as the key and the word as the value.
      A count of the array will give the number of words.
      You can then foreach() thru the array and do the special function.

      Who would want 10 char words contaminating their text 😉

        NogDog wrote:

        I'm confused: first you are discussing the counting of words, and then you are discussing the number of characters in a single word. 😕

        All this inserts into a form and displayed at a different page. I don't want users to just type in anything like: hahahahahahahahaha yyou suckkk blahblahblahbah.
        That's my reason for trying to count characters on a single word so I can not process the form if words with characters pass a certain amount. I know it won't catch everything, but it may filter some.

        I then thought how to count the words. I tried the preg_match but it counts everything. So I thought of str_word_count to break the words into an array and count the characters on the word. I'm only limiting 100 characters in the textarea.

          $maxWordLength = 15;
          if(preg_match('/[^-\s]{'.$maxWordLength+1.',}/', $text))
          {
             // error: excessively long word found
          }
          else
          {
             // OK, continue processing
          }
          
            $text = trim($_POST['textarea']);//gets text from textarea form
            $maxWordLength = 15;
            if(preg_match('/[^-\s]{'.$maxWordLength+1.',}/', $text))
            {
               // error: excessively long word found
            }
            else
            {
               // OK, continue processing
            }
            

            I seem to keep getting an error:
            Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING

            Can you explain that preg_match...thought it had to look something like this:
            preg_match('/pattern here/', 'subject')

            What is this part mean [-\s]? The ^ started it but no $ to end? Curly specifies the number of characters.

            Just trying to understand it some more. Thanks.

              Cut-and-paste code never works. There are parentheses missing around "$maxWordLength+1".

              Wales wrote:

              What is this part mean [-\s]? The ^ started it but no $ to end?

              [-\s] means "any character that is not a hyphen or whitespace".

                Oops! Weedpacket is right (as usual). For whatever reason I assumed that the "." operator has a lower precedence than the "+" operator, but in fact they are equivalent.

                  Write a Reply...