Kristian Rafteseth
Kristian Rafteseth

Reputation: 2032

How to encode foreign characters in url in PHP

How do you correctly encode an URL with foreign characters in PHP? I assumed urlencode() would do the trick but it does not.

The correct encoding for the following URL

http://eu.battle.net/wow/en/character/anachronos/Paddestøel/advanced

Is this:

http://eu.battle.net/wow/en/character/anachronos/Paddest%C3%B8el/advanced

But urlencode encodes it like this:

http://eu.battle.net/wow/en/character/anachronos/Paddest%F8el/advanced

What function do I use to encode it like on the second example?

Upvotes: 0

Views: 1057

Answers (3)

Jeroen de Jong
Jeroen de Jong

Reputation: 337

Use UTF-8 encoding

function url_encode($string){
    return urlencode(utf8_encode($string));
}

Then use this function to encode your url (got it in a comment here: http://php.net/manual/en/function.urlencode.php)

Upvotes: 1

deceze
deceze

Reputation: 522510

There is no "correct" encoding. URL-percent-encoding simply represents raw bytes. It's up to you what those bytes are or how you're going to interpret them later. If your string is UTF-8 encoded, the percent-encoded raw byte representation is %C3%B8. If your string is not UTF-8 encoded, it's something else. If you want %C3%B8, make sure your string is UTF-8 encoded.

Upvotes: 2

Álvaro González
Álvaro González

Reputation: 146573

Your PHP scripts seem to use some single-byte encoding. You can either:

In general, making the full switch to UTF-8 fixes all encoding issues at once but initial migration might require some extra work.

Upvotes: 2

Related Questions