问题
I am trying to recreate a string but I am having issues with the utf-8 encoding (I guess?)
I am using the code below to format a string
$pre_subject = (strip_tags(html_entity_decode($temp_subject)));
$pre_subject = str_replace('Â', '', $pre_subject);
$pre_subject = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $pre_subject);
The problem is that instead of getting the result as in the 1st sentence below, I get a result as the second one.
1st. summary: SHANGHAI room - Réunion
2nd. summary: SHANGHAI room - R'eunion
I need to keep the format as the first example, how can I modify my code for that?
回答1:
The string has UTF-8 characters. You need to either decode them, or tell whatever is reading it to use UTF-8 encoding.
So, this:
$pre_subject = (strip_tags(html_entity_decode($temp_subject)));
$pre_subject = str_replace('Â', '', $pre_subject); // if this was trying to fix the problem, remove it
$pre_subject = utf8_decode($pre_subject);
Or add this to <head>
:
<meta charset="utf-8">
回答2:
Make you PHP header like:
header('Content-Type: text/html; charset=utf-8');
also, when dealing with utf-8, if you need to do string replace use mb_str_replace and not str_replace
来源:https://stackoverflow.com/questions/45947976/utf-8-encoding-issue-in-php-special-characters