沃梦达 / 编程问答 / php问题 / 正文

UTF8 编码问题 - 有很好的例子

UTF8 Encoding problem - With good examples(UTF8 编码问题 - 有很好的例子)

本文介绍了UTF8 编码问题 - 有很好的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符编码问题,不知何故我设法将具有不同字符编码的数据保存到我的数据库 (UTF8) 下面的代码和输出显示了 2 个示例字符串及其输出方式.其中 1 个需要更改为 UTF8,另一个已经是.

I have the following character encoding issue, somehow I have managed to save data with different character encoding into my database (UTF8) The code and outputs below show 2 sample strings and how they output. 1 of them would need to be changed to UTF8 and the other already is.

我该如何/应该如何检查是否应该对字符串进行编码?例如我需要正确输出每个字符串,那么如何检查它是否已经是utf8或者是否需要转换?

我使用的是 PHP 5.2,mysql myisam 表:

I am using PHP 5.2, mysql myisam tables:

CREATE TABLE IF NOT EXISTS `entities` (
  ....
  `title` varchar(255) NOT NULL
  ....
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

<?php
$text = $entity['Entity']['title'];
echo 'Original : ', $text."<br />";
echo 'UTF8 Encode : ', utf8_encode($text)."<br />";
echo 'UTF8 Decode : ', utf8_decode($text)."<br />";
echo 'TRANSLIT : ', iconv("ISO-8859-1", "UTF-8//TRANSLIT", $text)."<br />";
echo 'IGNORE TRANSLIT : ', iconv("ISO-8859-1", "UTF-8//IGNORE//TRANSLIT", $text)."<br />";
echo 'IGNORE   : ', iconv("ISO-8859-1", "UTF-8//IGNORE", $text)."<br />";
echo 'Plain    : ', iconv("ISO-8859-1", "UTF-8", $text)."<br />";
?>

输出 1:

Original : France Télécom
UTF8 Encode : France Télécom
UTF8 Decode : France T�l�com
TRANSLIT : France Télécom
IGNORE TRANSLIT : France Télécom
IGNORE : France Télécom
Plain : France Télécom

输出 2:###

Original : Cond� Nast Publications
UTF8 Encode : Condé Nast Publications
UTF8 Decode : Cond?ast Publications
TRANSLIT : Condé Nast Publications
IGNORE TRANSLIT : Condé Nast Publications
IGNORE : Condé Nast Publications
Plain : Condé Nast Publications

感谢您花时间在这个上.字符编码和我相处得不太好!

Thanks for you time on this one. Character encoding and I don't get on very well!

更新:

echo strlen($string)."|".strlen(utf8_encode($string))."|";
echo (strlen($string)!==strlen(utf8_encode($string))) ? $string : utf8_encode($string);
echo "<br />";
echo strlen($string)."|".strlen(utf8_decode($string))."|";
echo (strlen($string)!==strlen(utf8_decode($string))) ? $string : utf8_decode($string);
echo "<br />";

23|24|Cond� Nast Publications
23|21|Cond� Nast Publications

16|20|France Télécom
16|14|France Télécom

推荐答案

这可能是 mb_detect_encoding() 的工作 函数.

This may be a job for the mb_detect_encoding() function.

根据我对它的有限经验,当用作通用编码嗅探器"时,它并不是 100% 可靠的 - 它会检查某些字符和字节值的存在以进行有根据的猜测 - 但在这种狭隘的情况下(它'只需要区分 UTF-8 和 ISO-8859-1 )它应该工作.

In my limited experience with it, it's not 100% reliable when used as a generic "encoding sniffer" - It checks for the presence of certain characters and byte values to make an educated guess - but in this narrow case (it'll need to distinguish just between UTF-8 and ISO-8859-1 ) it should work.

<?php
$text = $entity['Entity']['title'];

echo 'Original : ', $text."<br />";
$enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");

echo 'Detected encoding '.$enc."<br />";

echo 'Fixed result: '.iconv($enc, "UTF-8", $text)."<br />";

?>

对于不包含特殊字符的字符串,您可能会得到不正确的结果,但这不是问题.

you may get incorrect results for strings that do not contain special characters, but that is not a problem.

这篇关于UTF8 编码问题 - 有很好的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UTF8 编码问题 - 有很好的例子

基础教程推荐