tags:

views:

53

answers:

3

I have a huge text that i keep getting in a json format. When i receive them in json, for some special characters like ' " &copy, i receive them differently. i am using php and json to convert json into html. for example, i receive

' as \c101d (single quote) " as \c201d (opening quote) " as \c202d (closing quote)

I am planning to keep all the ', " into an array and use that array to replace the \c101d values in the text to ' or something like that so that it is easier to check the whole text in one command, replace all the special characters properly and display them correctly on my webpage.

Maybe some like $arr=array("\c101d"=>"'", "\c202d"=>""") and then call this array on the $text variable to check for characters similar to that in the array and do a string replace. I have the idea but coding-wise how do i achieve this? Appreciate any help.

A: 

¿Are you using json_encode() with the different option flags?

For the substring replacement you should use strtr()

Coquevas
A: 

str_replace should do what you want.

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

The str_replace function takes arrays as possible parameters for search and replace, so you could do something like:

$search = array( '\'' , '"', ...);
$replace = array( '\c101d', '\c201d', ...);
$text = str_replace($search, $replace, $text);
steven_desu
ummm.... i tried what you gave but it does not seem to change anything. well this is what is happening... when i look at the json that i getting back, i can see that the special character is \u201c but on the html page it is showing as “ and i am not able to replace them! FYI, i am getting back the NYTIMES recent news API in JSON format. Obviously i have interchanged $search and $replace because ur code is doing the code of what i am trying to do.
Scorpion King
A: 

SOLVED

Well this piece of code solved all the problems, including ' , " , and all other weird characters.

$newtext=mb_convert_encoding($text,  'HTML-ENTITIES','UTF-8');
Scorpion King