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..
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