Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2010, 08:54 PM   PM User | #1
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
REGEX help needed: is this redundant ??

Could I just get rid of the first of these lines ?
PHP Code:
$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
mOrloff is offline   Reply With Quote
Old 02-23-2010, 08:59 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
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.
MattF is offline   Reply With Quote
Old 02-23-2010, 09:11 PM   PM User | #3
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
Quote:
Originally Posted by MattF View Post
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 ??
Quote:
Originally Posted by MattF View Post
... 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

Last edited by mOrloff; 02-23-2010 at 09:14 PM..
mOrloff is offline   Reply With Quote
Old 02-23-2010, 09:30 PM   PM User | #4
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Quote:
Originally Posted by mOrloff View Post
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:

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

Quote:
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.
MattF is offline   Reply With Quote
Old 02-23-2010, 09:43 PM   PM User | #5
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
Thanks-a-bunch.
mOrloff is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:38 PM.


Advertisement
Log in to turn off these ads.