php - Replace multiple dashes with one dash -
i have string looks this:
something-------another--thing //^^^^^^^ ^^
i want replace multiple dashes single one.
so expected output be:
something-another-thing //^ ^
i tried use str_replace()
, have write code again every possible amount of dashes. how can replace amount of dashes single one?
for rizier:
tried:
$mystring = "something-------another--thing"; str_replace("--", "-", $mystring); str_replace("---", "-", $mystring); str_replace("----", "-", $mystring); str_replace("-----", "-", $mystring); str_replace("------", "-", $mystring); str_replace("-------", "-", $mystring); str_replace("--------", "-", $mystring); str_replace("---------", "-", $mystring); etc...
but string have 10000 of lines between 2 words.
use preg_replace
replace pattern.
$str = preg_replace('/-+/', '-', $str);
the regular expression -+
matches sequence of 1 or more hyphen characters.
if don't understand regular expressions, read tutorial @ www.regular-expression.info.
Comments
Post a Comment