JSON encoding Windows-1252 Unicode chars

While trying to use the json_encode function a Windows-1252 left and right quote character was breaking the encoding and I was ending up with no JSON. The string being encoded was from html within a PHP file. I’m guessing it was copy pasted from a Word doc.

To fix this the <a href="http://php.net/manual/en/function.mb-convert-encoding.php">mb_convert_encoding</a> function, not the <a href="http://php.net/manual/en/function.utf8-encode.php">utf8_encode</a> function, was needed. From the utf8_encode docs:  “This function will not convert such Windows-1252 characters correctly. Use a different function if Windows-1252 conversion is required”. mb_convert_encoding turned out to be the “different function”.

The function call ended up looking like this:
mb_convert_encoding($new_selection['new_string'], 'UTF8', array('Windows-1252'))

Leave a Reply

Your email address will not be published. Required fields are marked *