phpjsonunicodebackslashstripslashes

PHP - Replace JSON with the correct Unicode symbol


I have some JSON, that when decoded, I print out the result. Before the JSON is decoded, I use stripslashes() to remove extra slashes. The JSON contains website links, such as https://www.w3schools.com/php/default.asp and descriptions like Hello World, I have u00249999999 dollars

When I print out the JSON, I would like it to print out Hello World, I have $9999999 dollars, but it prints out Hello World, I have u00249999999 dollars.

I assume that the u0024 is not getting parsed because it has no backslash, though the thing is that the website links' forward slashes aren't removed through strip slashes, which is good - I think that the backslashes for the Unicode symbols are removed with stripslashes();

How do I get the PHP to automatically detect and parse the Unicode dollar sign? I would also like to apply this rule to every single Unicode symbol.


Solution

  • According to the PHP documentation on stripslashes (), it

    un-quotes a quoted string.

    Which means, that it basically removes all backslashes, which are used for escaping characters (or Unicode sequences). When removing those, you basically have no chance to be completely sure that any sequence as "u0024" was meant to be a Unicode entity, your user could just have entered that.

    Besides that, you will get some trouble when using stripslashes () on a JSON value that contains escaped quotes. Consider this example:

    {
      "key": "\"value\""
    }
    

    This will become invalid when using stripslashes () because it will then look like this:

    {
      "key": ""value""
    }
    

    Which is not parseable as it isn't a valid JSON object. When you don't use stripslashes (), all escape sequences will be converted by the JSON parser and before outputting the (decoded) JSON object to the client, PHP will automatically decode (or "convert") the Unicode sequences your data may contain.

    Conclusion: I'd suggest not to use stripslashes () when dealing with JSON entities as it may break things (as seen in the previous example, but also in your problem).