Say I have
sometext-somemore--test---test2
how could I have it replace all instaces where there are more than 1 - and have it replace them with just 1 so it would look like this
sometext-somemore-test-test2
Say I have
sometext-somemore--test---test2
how could I have it replace all instaces where there are more than 1 - and have it replace them with just 1 so it would look like this
sometext-somemore-test-test2
use a regex: s/-+/-/
The + says 'one or more of the preceding character'.
Use this:
echo preg_replace("/-+/","-","sometext-somemore--test---test2");
//prints sometext-somemore-test-test2
You could do this with a regular expression and the preg_replace
function :
$str = 'sometext-somemore--test---test2';
echo preg_replace('/-+/', '-', $str);
would give you :
sometext-somemore-test-test2
The pattern I used here : -+
-
+
And don't hesitate to read the PCRE Patterns section of the manual, if you are interested by regular expressions ;-)