First, you don't need to explode and then implode - just use a different 
var for the exploded array and use the original after the test.

My suggestion for testing (but you really must test it and see if it's 
faster or slower than your method) is using wordwrap() with your max 
word length and then check if any line is longer than your max allowed 
length. The advantage may be that you probably use 10-15 as the max 
length in real life and you would skip quite a few checks because you're 
going to end up with several words on a typical line. I guess it *might* 
be slightly fatser because wordwrap() is native to PHP - the job however 
is more complicated, so you may end up with a slower version than your 
current one.


Justin French wrote:

>hi all,
>looking for some advice on the best way to approach this:
>i have a guestbook running on a few sites, and occasionally we get
>"creative" people who think it's a good idea to post messages with really
>long words or URLs into the text, which totally mess up the layout of the
>what I need is an efficient way of checking the input for extremely long
>words (say about 60-odd characters +)
>I assume I just split the input by " " (space) into an array, and check that
>each "word" isn't longer than 60 chars, but this seems like a lot of work
>for the server... although I am limiting the entire input to 2000 chars, so
>maybe this isn't too much work for the server?
>this is what I'm using:
>$word_length = 5;
>$error = 0;
>$str = "cat dog bird mouse elephant"; // illegal
>$str = explode(" ", $str);
>foreach($str as $key => $word)
>    {
>    if(strlen($word) > $word_length)
>        { $error = 1; }
>    }
>    { echo "sorry"; }
>    {
>    $str = implode(" ", $str);
>    echo $str."<BR>";
>    }
>any ways to improve it?

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to