tags:

views:

168

answers:

3

I am trying to convert an underscore to dash in php.

hi i have a regular expression that converts white spaces to dash but how can i achieve underscore to dash?

 $item = preg_replace('/\ +/', '-', $item);
+2  A: 
preg_replace('/_/', '-', $item);
SilentGhost
+5  A: 

You don't need a regular expression. Use str_replace:

$item = str_replace('_', '-', $item);

Josh Leitzel
Or better yet, strtr(), because it's faster.
Reinis I.
A: 

You can convert both space and underscore to dash with one regex:

$item = preg_replace('/[ _]+/', '-', $item);
andho