Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP URLDecode / UTF8_Encode Character Set Issues with special characters

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?

like image 738
Marcus Avatar asked Sep 15 '25 01:09

Marcus


2 Answers

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 = Â)

like image 139
Wh1T3h4Ck5 Avatar answered Sep 17 '25 15:09

Wh1T3h4Ck5


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
like image 44
Kaivosukeltaja Avatar answered Sep 17 '25 15:09

Kaivosukeltaja