Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression for converting underscores to dash in php

Tags:

regex

php

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);
like image 832
pmarreddy Avatar asked Sep 28 '09 15:09

pmarreddy


Video Answer


2 Answers

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

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

like image 166
Josh Leitzel Avatar answered Oct 03 '22 05:10

Josh Leitzel


preg_replace('/_/', '-', $item);
like image 28
SilentGhost Avatar answered Oct 03 '22 06:10

SilentGhost