Is there a PHP script that can spot long individual words?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Is there a PHP script that can spot long individual words?

Post by simonmlewis »

We are getting a lot of user comments where users are enter thousands of !!!!!!!!!!!!!!!! in their comments.
While we are not making them live, it is causing havoc with the screen to check for the comments.

So is there a simple way to find the "longest word" in a string, and count how many characters it is. Thus stopping it from being posted if it is over say, 20 characters....??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Is there a PHP script that can spot long individual word

Post by Celauran »

You could explode the string on space to break it into individual words, strlen each words, and go from there. Might be a more elegant solution, but that's certainly dead simple.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Is there a PHP script that can spot long individual word

Post by Christopher »

simonmlewis wrote:We are getting a lot of user comments where users are enter thousands of !!!!!!!!!!!!!!!! in their comments.
While we are not making them live, it is causing havoc with the screen to check for the comments.

So is there a simple way to find the "longest word" in a string, and count how many characters it is. Thus stopping it from being posted if it is over say, 20 characters....??
Is is just exclamation points? If it's sentence end punctuation, you could this do before saving:

Code: Select all

$comment = str_replace(array('!!!!', '????', '....'), '', $comment);
(#10850)
Post Reply