简体   繁体   中英

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 ' " &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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM