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

    REGEX help needed: exclude all non-word except hyphen ??

    I am using this code:
    PHP Code:
    $text=preg_replace('/\W/'' '$body); // strip all punctiation and non-word characters 
    But have come to realize that I need to keep the hyphens.

    What is the easiest way to srtip all non-word characters except the hyphen?
    Is there a way to make an exception for "-" while using \W, or some other direct way of doing it?

    ~ Mo
    Last edited by mOrloff; 01-21-2010 at 08:51 PM. Reason: Made the title easier on the eyes

  • #2
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    I figured it out. Thanks anyhow.
    I changed it to:
    PHP Code:
    $text=preg_replace('/[^A-Za-z0-9-]/'' '$body); // strip all punctuation and non-word characters 
    ~Mo

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Could have also used /[\W-]/
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

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