Remove Multiple Hyphen From String (php)

Pedro Chagas
Pedro Chagas used Ask the Experts™
on
Hi Expert's,
For remove multiple spaces in a string I use this code:
$var = preg_replace('/\s+/', ' ',$var);

Open in new window

What changes I have to do in the code, but for remove multiple hyphens?
Examples: this-is---a--test, convert to this-is-a-test;
this is a ---- test, convert to this is a - test;

The best regards,
JC
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
preg_replace parameters are:
1. SEARCH FOR
2. REPLACE WITH
3. WITHIN TEXT

So you would copy your existing line and underneath it:
$var = preg_replace('/-+/', '-',$var);

Open in new window


The -+ means "one or more of the - character"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial