简体   繁体   English

PHP中的UTF-8编码问题,特殊字符

[英]UTF-8 encoding issue in php, special characters

I am trying to recreate a string but I am having issues with the utf-8 encoding (I guess?) 我正在尝试重新创建字符串,但是utf-8编码存在问题(我猜是吗?)

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? 我需要将格式保留为第一个示例,如何为此修改代码?

The string has UTF-8 characters. 该字符串包含UTF-8字符。 You need to either decode them, or tell whatever is reading it to use UTF-8 encoding. 您需要对其进行解码,或者告诉正在读取的内容以使用UTF-8编码。

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> : 或将其添加到<head>

<meta charset="utf-8">

Make you PHP header like: 使您的PHP标头像:

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 另外,在处理utf-8时,如果需要执行字符串替换,请使用mb_str_replace而不是str_replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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