我想用单个短划线替换任何出现的空格,如果有多个空格,那么它应该只用一个短划线替换
例如:
$string = "testing string" //should be "testing-string"
$string = "testing string" //should be "testing-string"
$string = "testing\t\t\n\n string" //should be "testing-string"
我试过这个:
$string = "this is testing string\n\n\nxyz\t\t\tabc";
echo preg_replace('![\s+|\t+|\n+]!', "-" , $string);
但问题是用单个破折号替换每个空格
答案 0 :(得分:2)
你写的模式错了,你真正需要的是这个[\s]+
检查出来:
$string = "this is testing string\n\n\nxyz\t\t\tabc";
echo preg_replace('![\s]+!', "-" , $string);
答案 1 :(得分:0)
echo preg_replace('/(\s|\t|\n)+/ui', "-" , $string);