本文介绍了使用 PHP 将 URLS 中的空格替换为 %20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望用 %20 替换 url 中的所有空格实例.我将如何使用正则表达式做到这一点?
I'm looking to replace all instances of spaces in urls with %20. How would I do that with regex?
谢谢!
推荐答案
这里不需要正则表达式,如果你只是想用另一个替换一段字符串:使用 str_replace()
应该足够了:
No need for a regex here, if you just want to replace a piece of string by another: using str_replace()
should be more than enough :
$new = str_replace(' ', '%20', $your_string);
但是,如果您想要更多,并且您可能会这样做,如果您正在使用 URL,您应该查看 urlencode()
函数.
这篇关于使用 PHP 将 URLS 中的空格替换为 %20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!