开发者

how to prevent the url to be transformed?

i have a url, the parameters in the url is human meaningful. but after transformed the url contains many characters like "%5B%5D",开发者_StackOverflow中文版 how should i prevent the url to be transformed.


%NN is the only correct way to represent non-ascii chars in urls.

You cannot prevent it. If a client cannot (or don't want) to present them in a "right" way - they will be %'ed.


Example title:

$title = 'Exämplé strîng ìnclüding spéciâl chàrãctêrs and (söme) [brackets].';

Valid URL encoding:

$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.

The urlencode() function encodes every non-ascii character.
URLs must be encoded this way in order to work properly.
Fortunately you can make it human readable with something like this:
(Remove non-ascii characters / Replace spaces by underscores)

$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

In conclusion, create URLs like:

"http://www.site.com/blog.php?Article=Example_string_including_special_characters_and_some_brackets"

Instead of:

"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."


Actually, it does concern with the rendering engine and you can't escape from it. For something like gecko, in spite being transformed, it can be shown in human readable form.


Are you talking about when you display the URL, or when you use it as a href value?

If you want it to display nicely, you can run it through urldecode(). As per other answers, inside a href value, it has to look like that, or the URL can break in some browsers and servers, so it's a good idea to leave it looking like that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜