Forum Moderators: coopster

Message Too Old, No Replies

Regex: measuring strings of non-whitespace chars

Detecting length of character strings for wrapping purposes

         

Sargant

11:24 am on Apr 4, 2004 (gmt 0)

10+ Year Member



I want to check that any user-submitted string does not contain a single "word" of more than, for example, 15 characters. If it does, I then want to capture that string. I use "word" in quotes as this applies to any string of non-whitespace characters (i.e. characters that don't cause new line wrapping - so the \s character instead of the \b character).

Being new to regular expressions, the expression I have tried to use is


\s([\S]{15,})\s

which doesn't seem to work. The various regexp archives I have browsed don't seem to have solutions for this problem.

Any help would be greatly appreciated.

Birdman

11:42 am on Apr 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use the explode function to split the string into an array. Then loop the array and test the length.

$arr = explode(" ",$string);

for ($i = 0; $i < count($arr); $i++){
if (strlen($arr[$i]) > 15){
//do stuff to long words here
}
}