Remove accents from a JSON response using the raw content.
Posted
by Pentium10
on Stack Overflow
See other posts from Stack Overflow
or by Pentium10
Published on 2010-05-13T12:13:14Z
Indexed on
2010/05/13
12:34 UTC
Read the original article
Hit count: 384
This is a follow up of this question: Remove accents from a JSON response.
The accepted answer there works for a single item/string of a raw JSON content. But I would like to run a full transformation over the entire raw content of the JSON without parsing each object/array/item.
What I've tried is this
function removeAccents($jsoncontent) {
$obj=json_decode($jsoncontent); // use decode to transform the unicode chars to utf
$content=serialize($obj); // serialize into string, so the whole obj structure can be used string as a whole
$a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr';
$b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
$content=utf8_decode($content);
$jsoncontent = strtr($content, $a, $b);
// at this point the accents are removed, and everything is good
echo $jsoncontent;
$obj=unserialize($jsoncontent); // this unserialization is returning false, probably because we messed up with the serialized string
return json_encode($obj);
}
As you see after I decoded JSON content, I serialized the object to have a string of it, than I remove the accents from that string, but this way I have problem building back the object, as the unserialize stuff returns false.
How can I fix this?
© Stack Overflow or respective owner