本文介绍了仅用一个“ _”代替空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试仅用一个下划线替换所有连续的空格;通过使用以下代码行,我可以轻松地用 _替换一个空格:
I'm trying to replace all of the consecutive spaces with just one underscore; I can easily replace one space with "_" by using the following line of code:
str_replace(" ", "_",$name);
Evan我可以通过以下代码行用 _替换一个空格:
Evan I can replace one spaces with "_" by following line of code:
str_replace(" ", "_",$name);
但是问题是我不知道要检查多少空格!
But the problem is I don't know how many blank spaces I have to check!
如果我的问题不清楚,请告诉我您需要进一步澄清。
If my question is not clear please let me know which part you need more clarification.
谢谢
推荐答案
可能是最干净,最易读的解决方案:
Probably the cleanest and most readable solution:
preg_replace('/[[:space:]]+/', '_', $name);
这将用单个下划线替换所有空格(无论多少)。
This will replace all spaces (no matter how many) with a single underscore.
这篇关于仅用一个“ _”代替空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!