1

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

3
  • 1
    On what platform/language you are working?
    – Hossein
    Apr 24, 2011 at 10:37
  • The programming language is php
    – David
    Apr 24, 2011 at 10:40
  • I know what you're talking about, It's the same with Hebrew in parameters. I would like a solution as well...
    – fingerman
    Apr 24, 2011 at 11:09

4 Answers 4

3

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

1

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

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.