Search code examples
phpmysqlutf-8utf8-decode

php utf8_decode doesn't recognize square root from $_GET


When i send this perfect square character:

25²

And retrieve it in another page using utf8_decode i get (as expected):

25²

Now i want to do the same thing with square roots. When i send this square root:

√225

And retrieve (_$GET) it using utf8_decode as before i get:

?255

Note: i would prefer a method that will solve this when i retrieve the value (like √225) and not a method that will force me to change the initial representation of it.

Edit: I thought i was using mysql but i'm not. sorry for misleading. this is the script i use to retrieve the value:

if (isset($_GET['json'])){  
  $json = urldecode($_GET['json']);
  $roots = json_decode($json, true);
  $arrList = utf8_decode(implode(",,,", $roots['nameList']));
  print_r($arrList)
}

Solution

  • The utf8_decode function converts a string from UTF-8 to latin1. The square root symbol is not available in latin1, so utf8_decode substitutes it with a question mark. This doesn't happen with ² because it does exist in latin1, in position 0xB2. (You can check which characters are available in the page of latin1 in Wikipedia)

    A possible solution is removing the call to utf8_decode and leave the text as UTF-8, but then you have to make sure the rest of your code can handle UTF-8.