PHP URLDecode / UTF8_Encode Character Set Issues with special characters

11,269

Solution 1

I don't think ord() is multibyte compatible. It's probably returning only the code for the first character in the string, which is Â. Try to utf8_decode() the string before calling ord() on it and see if that helps.

ord(utf8_decode(urldecode("%C2%A3"))); // This returns 163

Solution 2

if you try

var_dump(urldecode("%C2%A3"));

you'll see

string(2) "£"

because this is 2-byte character and ord() returns value of first one (194 = Â)

Solution 3

Some infos about urldecode and UTF-8 can be found in the first comment of the urldecode documentation. It seems to be a known problem.

Share:
11,269
Marcus
Author by

Marcus

Updated on June 04, 2022

Comments

  • Marcus
    Marcus almost 2 years

    I'm passing a pound symbol £ to a PHP page which has been URLEncoded by ASP as %C2%A3.

    The problem:

    urldecode("%C2%A3") // £
    ord(urldecode("%C2%A3")) // get the character number - 194
    ord("£") // 163  - somethings gone wrong, they should match
    

    This means when I do utf8_encode(urldecode("%C2%A3")) I get £

    However doing utf8_encode("£") I get £ as expected

    How can I solve this?