使用PHP将URL中的SPACES替换为%20 [英] Using PHP Replace SPACES in URLS with %20

查看:177
本文介绍了使用PHP将URL中的SPACES替换为%20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用%20替换url中所有空格实例.我该如何使用正则表达式呢?

I'm looking to replace all instances of spaces in urls with %20. How would I do that with regex?

谢谢!

推荐答案

如果您只想用另一个字符串替换这里,则不需要正则表达式:使用

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,那么应该看看


But, if you want a bit more than that, and you probably do, if you are working with URLs, you should take a look at the urlencode() function.

这篇关于使用PHP将URL中的SPACES替换为%20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆