Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts

    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

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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);

    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.

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Thanks-a-bunch.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •