如何使用 PHP 将多个空格(如果超过一个)转换为单个破折号

How to convert a number of spaces(if more than one) to single dash using PHP

我可以将 Space 转换为破折号。

$string = str_replace(' ', '-', $string);

但是我想转换多个空格;二、三、四、五、六或很快.. 空格变成单破折号

变成 -

使用 preg_replace

试试这个 regex
$string = preg_replace( '!\s+!', '-', $string );
 preg_replace('/\/s+/g', '-', $str);

你可以用 preg_replace():

preg_replace('/[[:space:]]+/', '-', $string);