I have a string, I want to convert multiple appearances of -
to just one -
.
I have tried preg_replace('/--+/g', '-', $string)
but that simply returns nothing..
You should not use g
in the pattern, and you can simplify your regular expression:
preg_replace('/-+/', '-', $string);
Backslash escapes are not required.
<?
$string = "asdfsdfd----sdfsdfs-sdf-sdf";
echo preg_replace('/-+/', '-', $string);
?>
Output:
asdfsdfd-sdfsdfs-sdf-sdf
preg_replace('/([\-]+)/', '-', $string)
Your code gives the following error:
Warning: preg_replace(): Unknown modifier 'g'
There is no g
modifier. Try:
preg_replace('/--+/', '-', $string)