PHP:fopen(UrlWithWhiteSpaces)
正如您在标题上看到的,我正在尝试PHP:fopen(UrlWithWhiteSpaces),php,url,fopen,Php,Url,Fopen,正如您在标题上看到的,我正在尝试fopen()一个带有空格的Url。其他stackoverflow帖子并没有起到真正的作用,可能是因为它们已经过时了吗? 我试过: URL编码 拉乌尔编码 以上都不起作用,http://test.com/two -words.jpg这给了我: urlencode:http%3A%2F%2Ftest.com%2ftou+-+words.jpg rawurlencode:http%3A%2F%2Ftest.com%2four%20-%20words.jpg 当
fopen()
一个带有空格的Url。其他stackoverflow帖子并没有起到真正的作用,可能是因为它们已经过时了吗?我试过:
- URL编码
- 拉乌尔编码
http://test.com/two -words.jpg
这给了我:
- urlencode:
http%3A%2F%2Ftest.com%2ftou+-+words.jpg
- rawurlencode:
http%3A%2F%2Ftest.com%2four%20-%20words.jpg
http://test.com/two%20-%20words.jpg
,这是您键入http://test.com/two -words.jpg
并点击回车键。从
空间
到%20
和全部进行转换时,我必须使用哪个函数
我可能需要的其他可能的转换(我想不出更多了,但我很确定它们存在,可能在特殊符号上)?使用:
$newUrl = preg_replace('/ /', '%20', 'http://test.com/two - words.jpg');
echo $newUrl;
$newUrl = str_replace(' ', '%20', 'http://test.com/two - words.jpg');
输出
http://test.com/two%20-%20words.jpg
http://test.com/two%20-%20%5B%22word%3C%3E%3Cs.jpg
或者:
$newUrl = preg_replace('/ /', '%20', 'http://test.com/two - words.jpg');
echo $newUrl;
$newUrl = str_replace(' ', '%20', 'http://test.com/two - words.jpg');
但更一般地说,您需要编码的不仅仅是空间。我做了这个函数,如果你不想使用或,这就是你想要的:
function encodeURI($URI)
{
return str_replace(array('%', '^', '+', '{', '[', '}', ']', '"', '|', '\\', '<', '>', ' '),
array('%25', '%5E', '%2B', '%7B', '%5B', '%7D', '%5D', '%22', '%7C', '%5C', '%3C', '%3E', '%20'), $URI);
}
你好!你确定这是我唯一需要的转换吗?我对此表示怀疑,但我会使用它,因为我没有更好的选择。这就是你所要求的。
和所有其他可能需要的转换
regex,而不是str\u replace
?为什么要增加开销?看看@dtbarne谢谢,这正是我需要的