Answer the question
In order to leave comments, you need to log in
How to write a regular expression to replace (space hyphen space "-") with hyphen "-"?
$text = ' Длинный объемный текст с ----- --- лишние пробелами -- африка ';
$text1 = preg_replace('|[\s]+|s', ' ', $text);
$text2 = preg_replace('|-[\s]+|s', '-', $text1);
$text3 = preg_replace('|[\s]-+|s', '-', $text2);
echo $text3; // Длинный объемный текст с-лишние пробелами-африка
Answer the question
In order to leave comments, you need to log in
Haven't tested it, but it looks like it should work. Do not know where you got | as limiters. In every language I know, regex delimiters are / . Also it is not necessary to produce variables beyond necessity.
$text = preg_replace('/[\s]+/s', ' ', $text);
$text = preg_replace('/[\s\-]*-[\s\-]*/s', '-', $text);
echo $text ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question