UTF-8 encoding issue in php, special characters

為{幸葍}努か 提交于 2020-01-07 04:15:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!