繁体   English   中英

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

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

我正在尝试重新创建字符串,但是utf-8编码存在问题(我猜是吗?)

我正在使用下面的代码来格式化字符串

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

问题是,我没有得到下面第一句中的结果,而是得到了第二个结果。

1st. summary: SHANGHAI room - Réunion 
2nd. summary: SHANGHAI room - R'eunion

我需要将格式保留为第一个示例,如何为此修改代码?

该字符串包含UTF-8字符。 您需要对其进行解码,或者告诉正在读取的内容以使用UTF-8编码。

所以这:

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

或将其添加到<head>

<meta charset="utf-8">

使您的PHP标头像:

header('Content-Type: text/html; charset=utf-8');

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

暂无
暂无

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

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