...

View Full Version : REGEX help needed: is this redundant ??



mOrloff
02-23-2010, 08:54 PM
Could I just get rid of the first of these lines ?


$text=preg_replace('/[^A-Za-z0-9-]/', ' ', $body); // strip all non-word, non-number, non-hyphen characters & punctuation
$text=preg_replace('/\b[^A-Za-z0-9-]{1,2}\b/', ' ', $text); // strip all words with fewer than three (min=1, max=2) characters

Can you think of any instance where eliminating it would potentially miss something?

~ Mo

MattF
02-23-2010, 08:59 PM
Not unless you want any string above three characters in length to be able to contain any character whatsoever.They're both parsing different strings too, btw.

mOrloff
02-23-2010, 09:11 PM
Not unless you want any string above three characters in length to be able to contain any character whatsoever...
I presume that's an answer to my first question about eliminating the 1st line, rather than my second question about potentially missing something ??

... They're both parsing different strings too, btw.
Yeah, they're actually in sequence exactly like that.

It grabs a copy of body, then replaces any non-desired char's with a space, then replaces any string of <3 of those same non-desired chars with a space.

~ Mo

MattF
02-23-2010, 09:30 PM
I presume that's an answer to my first question about eliminating the 1st line, rather than my second question about potentially missing something ??

Yup. You need both of those lines if you want your text to be alphanumeric. If you don't mind allowing underscores also, you could just change that first line to:



$text=preg_replace('/[^\w\-]/', ' ', $body);





Yeah, they're actually in sequence exactly like that.

It grabs a copy of body, then replaces any non-desired char's with a space, then replaces any string of <3 of those same non-desired chars with a space.

I'd overlooked the $text assignment. Muppet mode there. :D

mOrloff
02-23-2010, 09:43 PM
Thanks-a-bunch.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum