array of special characters to replace in a text, php, json
I have a huge text that i keep getting in a json format. When i receive them in json, for some special characters like ' " ©, 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 a开发者_开发技巧ny help.
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');
¿Are you using json_encode()
with the different option flags?
For the substring replacement you should use strtr()
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);
精彩评论