View Full Version : Resolved REGEX help needed: exclude all non-word except hyphen ??

01-21-2010, 08:22 PM
I am using this 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

01-21-2010, 09:51 PM
I figured it out. Thanks anyhow.
I changed it to:

$text=preg_replace('/[^A-Za-z0-9-]/', ' ', $body); // strip all punctuation and non-word characters


01-22-2010, 03:50 AM
Could have also used /[\W-]/