使用 PHP 将 URL 中的空格替换为 %20
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9240556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHP Replace SPACES in URLS with %20
提问by David
I'm looking to replace all instances of spaces in urls with %20. How would I do that with regex?
我希望用 %20 替换 url 中的所有空格实例。我将如何用正则表达式做到这一点?
Thank you!
谢谢!
回答by Pascal MARTIN
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 :
这里不需要正则表达式,如果您只想用另一个字符串替换一段字符串:使用str_replace()
应该绰绰有余:
$new = str_replace(' ', '%20', $your_string);
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.
但是,如果您想要更多,并且您可能会这样做,如果您正在使用 URL,则应该查看该urlencode()
函数。
回答by Madara's Ghost
Use urlencode()
rather than trying to implement your own. Be lazy.
使用urlencode()
而不是尝试实现自己的。偷懒。
回答by drosanda
I think you must use rawurlencode() instead urlencode() for your purpose.
我认为您必须使用 rawurlencode() 而不是 urlencode() 来达到您的目的。
sample
样本
$image = 'some images.jpg';
$url = 'http://example.com/'
With urlencode($str) will result
使用 urlencode($str) 将导致
echo $url.urlencode($image); //http://example.com/some+images.jpg
its not change to %20 at all
它根本没有更改为 %20
but with rawurlencode($image) will produce
但是使用 rawurlencode($image) 会产生
echo $url.rawurlencode(basename($image)); //http://example.com/some%20images.jpg
回答by Vyktor
You've got several options how to do this, either:
您有多种选择来执行此操作,或者:
urlencode()
orrawurlencode()
- functions designed to encode URLs for http protocolstr_replace()
- "heavy machinery" string replacestrtr()
- would have better performance thanstr_replace()
when replacing multiple characterspreg_replace()
use regular expressions (perl compatible)
urlencode()
或rawurlencode()
- 旨在为 http 协议编码 URL 的函数str_replace()
- “重型机械”字符串替换strtr()
- 会比str_replace()
替换多个字符时有更好的性能preg_replace()
使用正则表达式(perl 兼容)
strtr()
strtr()
Assuming that you want to replace "\t"
and " "
with "%20"
:
假设你要替换"\t"
,并" "
用"%20"
:
$replace_pairs = array(
"\t" => '%20',
" " => '%20',
);
return strtr( $text, $replace_pairs)
preg_replace()
preg_replace()
You've got few options here, either replacing just space ~ ~
, again replacing space and tab ~[ \t]~
or all kinds of spaces~\s~
:
你在这里几乎没有选择,要么只替换 space ~ ~
,再次替换 space 和 tab~[ \t]~
或所有类型的空格~\s~
:
return preg_replace( '~\s~', '%20', $text);
Or when you need to replace string like this "\t \t \t \t"
with just one %20
:
或者当你需要"\t \t \t \t"
用一个替换这样的字符串时%20
:
return preg_replace( '~\s+~', '%20', $text);
I assumed that you really want to use manual string replacement and handle more types of whitespaces such as non breakable space (
)
我假设您真的想使用手动字符串替换并处理更多类型的空格,例如不可破坏的空格 (
)
回答by alex347
$result = preg_replace('/ /', '%20', 'your string here');
you may also consider using
你也可以考虑使用
$result = urlencode($yourstring)
to escape other special characters as well
也可以转义其他特殊字符
回答by Jonatas Walker
public static function normalizeUrl(string $url) {
$parts = parse_url($url);
return $parts['scheme'] .
'://' .
$parts['host'] .
implode('/', array_map('rawurlencode', explode('/', $parts['path'])));
}