[英]PHP replace all spaces with hyphens
I replace all spaces with hyphens in my script like this: 我在我的脚本中用连字符替换所有空格,如下所示:
$final = str_replace(' ','-',$final); The - is the only thing I replace.
What I'm replacing are Band Name - Song Name
So in this case, if I had something like: 我正在取代的是Band Name - Song Name
所以在这种情况下,如果我有类似的东西:
Metallica - Hero of the Day
I end up with: 我最终得到:
Metallica---Hero-of-the-Day
Notice the 3 ---
there? 请注意3 ---
那里?
Can you suggest an elegant way of ending up with just one -
instead of 3. 你能建议一种优雅的方式,只用一个-
而不是3个。
I can keep doing str_replace until it's done, but that doesn't see right. 我可以继续做str_replace直到它完成,但那看起来不对。
使用正则表达式使用一个连字符更改多个空格或连字符:
$final = preg_replace('#[ -]+#', '-', $text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.