我有一个网址,网址中的参数对人类有意义。但转换后的网址包含许多字符,如"%5B%5D",我应该如何防止网址被转换。
发布于 2011-04-24 19:09:47
%NN是在urls中表示非ascii字符的唯一正确方法。
你不能阻止它。如果客户端不能(或不想)以“正确”的方式呈现它们-它们将被%'ed。
发布于 2011-04-24 19:36:41
示例标题:
$title = 'Exämplé strîng ìnclüding spéciâl chàrãctêrs and (söme) [brackets].';有效的URL编码:
$title = 'Exämplé strîng ìnclüding spéciâl chàrãctêrs and (söme) [brackets].';
$title = urlencode($title) ;
// Result: Ex%C3%A4mpl%C3%A9+str%C3%AEng+%C3%ACncl%C3%BCding+sp%C3%A9ci%C3%A2l+ch%C3%A0r%C3%A3ct%C3%AArs+and+%28s%C3%B6me%29+%5Bbrackets%5D.函数的作用是:对每个非ascii字符进行编码。
URL必须以这种方式编码才能正常工作。
幸运的是,你可以用下面这样的代码让它变得可读:
(删除非ascii字符/用下划线替换空格)
$title = 'Exämplé strîng ìnclüding spéciâl chàrãctêrs and (söme) [brackets].';
$title = iconv('UTF-8', 'US-ASCII//TRANSLIT', $title);
$title = preg_replace('/[^A-Za-z0-9 ]/', '', $title );
$title = str_replace(' ','_',$title);
// Result: Example_string_including_special_characters_and_some_brackets总之,创建如下URL:
"http://www.site.com/blog.php?Article=Example_string_including_special_characters_and_some_brackets"而不是:
"http://www.site.com/blog.php?Article=Ex%C3%A4mpl%C3%A9+str%C3%AEng+%C3%ACncl%C3%BCding+sp%C3%A9ci%C3%A2l+ch%C3%A0r%C3%A3ct%C3%AArs+and+%28s%C3%B6me%29+%5Bbrackets%5D."发布于 2011-04-24 19:44:05
实际上,它确实与渲染引擎有关,您无法摆脱它。对于像壁虎这样的东西,尽管经过了变换,它仍然可以以人类可读的形式显示出来。
https://stackoverflow.com/questions/5770055
复制相似问题