I have an array of arrays. Each inner array has string elements.
One of the strings contain: –
This causes
Exception 'PHPErrorException' with message 'Warning [2] json_encode(): Invalid UTF-8 sequence in argument
when I call json_encode
. If the character is not present, the function call works. How could I make sure that any characters are supported by json_encode
, or not supported characters are ignored? I have been playing around with iconv
and all kinds of conversion but to no avail.
Have you tried iterating your array and running htmlentities
on each string within before passing to json_encode
?
If the problem is with the dash then html encoding it may help (although there are several different types of dash, so would help to know which dash specifically PHP is falling over on).
As pointed out below, may need ENT_SUBSTITUTE
(PHP 5.4+)
You are using en dash
. Please look here.
Why don't you try str_replace()
using this character –
and its corresponding HTML Number –