Encode UTF-8 for URLs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Encode UTF-8 for URLs

Post by shiznatix »

I know normally one could use rawurlencode() or just urlencode() but alas, these are not working for me.

Basically, I have a string that is UTF-8 encoded with the fancy characters like so:
[text]Gävle[/text]

Now, I need to convert this string to (I don't know the encoding name) look like this:
[text]G%E4vle[/text]

rawurlencode and urlencode both are not encoding the ä at all. How can I do this?

Edit
Just to clarify, it seams that something is working but just not well. For example, I have the string:
[text]Värtavägen 25, Stockholm, Stockholm län, 11553, Sverige[/text]
if I use urlencode() and just print that out to the browser, I get:
[text]V%C3%A4rtav%C3%A4gen+25%2C+Stockholm%2C+Stockholm+l%C3%A4n%2C+11553%2C+Sverige[/text]
which is correct. The problem though is that as soon as I put that into a link, it removes the %C3 type characters (ä) and replaces them with their original ä insanity. I literally have the link:
[text]http://maps.google.se/maps?q=V%C3%A4rta ... -1&iwloc=A[/text]
and as soon as I put this in an address bar, it gets converted to the actual characters which are invalid in a URL. How can I stop this from happening?

Sidenote: I suppose this isn't a PHP question anymore so this post can be moved
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Encode UTF-8 for URLs

Post by tr0gd0rr »

Google maps is using the "iso-8859-1" as the charset. If you change it to "ie=utf-8" google properly displays the location.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Encode UTF-8 for URLs

Post by shiznatix »

tr0gd0rr wrote:Google maps is using the "iso-8859-1" as the charset. If you change it to "ie=utf-8" google properly displays the location.
Ha, I had no idea. Thanks a million
Post Reply