4
Today I came across this mistake:
When trying to encode an array for json, some values return null. I arrived at the solution before using the function json_encode() pass all strings to UTF-8 with htmlentities($string, UTF-8) but in a special case, a string had this character - (notice that it is different from a hyphen -), and returned me error. look at the tests I did:
$string = htmlentities('Av. 7 de setembro – Salvador', UTF-8); // String com o caracter que retorna erro
$string2 = htmlentities('Av. 7 de setembro - Salvador', UTF-8); // String com o Hífen
$array = array('string' => $string, 'string2' => $string2);
echo json_encode($array);
Output: {"string":null,"string2":"Av. 7 de setembro - Salvador"}
Is there another way to convert this character? I’m afraid there are other characters that htmlentities() will not solve.
In my case, if I remove the htmlentities the other characters as input... will return NULL
– Diego Henrique
Ai @Diegohenrique is your page that has to put the correct header...
– user6026
The application was designed in ISO-8859-1, I agree that utf-8 would be much better to work with! Thanks for the help!
– Diego Henrique
@Diegohenrique is only put to the encoding so it is often done so by putting the header on the page, especially when working with ajax! in case only switch to ISO-8859-1
– user6026
@Diegohenrique when working with bank will be required to use this header
– user6026