【问题标题】:Encoding To Utf8, I have some ÂÂ编码为 Utf8,我有一些
【发布时间】:2013-09-11 15:06:00
【问题描述】:

几周前我意识到我们网站上的一些旧列表中有一些奇怪的字符

我有一些像这样的字符  等于双引号 (") 我有一些像â这样的角色??

我想修复它们,但我不知道如何。

自从我使用 preg_replace 如果我制作此代码时

$text = 'this is a listingTitle  with an encoding problem';
$test = preg_replace("/ÂÂ/",'"',$text);
echo$test;

$test 仍然会在字符串中出现编码错误,所以就像 preg_replace 看不到编码问题一样。

有没有人知道我可以做些什么来修复这些编码错误? 或者有什么方法我应该继续

谢谢

在此处编辑--------------------------------------------- -----

好的,我现在要做的是将每个字符转换回 utf8 而且由于某种原因,我的字符串混合了一些编码..

所以这里是一个字符串和构成该字符串的字符的细分。

Milwaukee 2415-21 M12 无绳锂离子 3/8Â 直角钻/起子套件带

这就是故障

atChar[0] = 'M' encoding is = ASCII
atChar[1] = 'i' encoding is = ASCII
atChar[2] = 'l' encoding is = ASCII
atChar[3] = 'w' encoding is = ASCII
atChar[4] = 'a' encoding is = ASCII
atChar[5] = 'u' encoding is = ASCII
atChar[6] = 'k' encoding is = ASCII
atChar[7] = 'e' encoding is = ASCII
atChar[8] = 'e' encoding is = ASCII
atChar[9] = ' ' encoding is = ASCII
atChar[10] = '2' encoding is = ASCII
atChar[11] = '4' encoding is = ASCII
atChar[12] = '1' encoding is = ASCII
atChar[13] = '5' encoding is = ASCII
atChar[14] = '-' encoding is = ASCII
atChar[15] = '2' encoding is = ASCII
atChar[16] = '1' encoding is = ASCII
atChar[17] = ' ' encoding is = ASCII
atChar[18] = 'M' encoding is = ASCII
atChar[19] = '1' encoding is = ASCII
atChar[20] = '2' encoding is = ASCII
atChar[21] = ' ' encoding is = ASCII
atChar[22] = 'C' encoding is = ASCII
atChar[23] = 'o' encoding is = ASCII
atChar[24] = 'r' encoding is = ASCII
atChar[25] = 'd' encoding is = ASCII
atChar[26] = 'l' encoding is = ASCII
atChar[27] = 'e' encoding is = ASCII
atChar[28] = 's' encoding is = ASCII
atChar[29] = 's' encoding is = ASCII
atChar[30] = ' ' encoding is = ASCII
atChar[31] = 'L' encoding is = ASCII
atChar[32] = 'i' encoding is = ASCII
atChar[33] = 't' encoding is = ASCII
atChar[34] = 'h' encoding is = ASCII
atChar[35] = 'i' encoding is = ASCII
atChar[36] = 'u' encoding is = ASCII
atChar[37] = 'm' encoding is = ASCII
atChar[38] = '-' encoding is = ASCII
atChar[39] = 'I' encoding is = ASCII
atChar[40] = 'o' encoding is = ASCII
atChar[41] = 'n' encoding is = ASCII
atChar[42] = ' ' encoding is = ASCII
atChar[43] = '3' encoding is = ASCII
atChar[44] = '/' encoding is = ASCII
atChar[45] = '8' encoding is = ASCII
atChar[46] = 'Â' encoding is = UTF-8
atChar[47] = '' encoding is = 
atChar[48] = ' ' encoding is = ASCII
atChar[49] = 'R' encoding is = ASCII
atChar[50] = 'i' encoding is = ASCII
atChar[51] = 'g' encoding is = ASCII
atChar[52] = 'h' encoding is = ASCII
atChar[53] = 't' encoding is = ASCII
atChar[54] = ' ' encoding is = ASCII
atChar[55] = 'A' encoding is = ASCII
atChar[56] = 'n' encoding is = ASCII
atChar[57] = 'g' encoding is = ASCII
atChar[58] = 'l' encoding is = ASCII
atChar[59] = 'e' encoding is = ASCII
atChar[60] = ' ' encoding is = ASCII
atChar[61] = 'D' encoding is = ASCII
atChar[62] = 'r' encoding is = ASCII
atChar[63] = 'i' encoding is = ASCII
atChar[64] = 'l' encoding is = ASCII
atChar[65] = 'l' encoding is = ASCII
atChar[66] = '/' encoding is = ASCII
atChar[67] = 'D' encoding is = ASCII
atChar[68] = 'r' encoding is = ASCII
atChar[69] = 'i' encoding is = ASCII
atChar[70] = 'v' encoding is = ASCII
atChar[71] = 'e' encoding is = ASCII
atChar[72] = 'r' encoding is = ASCII
atChar[73] = ' ' encoding is = ASCII
atChar[74] = 'K' encoding is = ASCII
atChar[75] = 'i' encoding is = ASCII
atChar[76] = 't' encoding is = ASCII
atChar[77] = ' ' encoding is = ASCII
atChar[78] = 'w' encoding is = ASCII

我现在能做什么?

【问题讨论】:

  • 如果将字符串解码为 ISO 格式会发生什么? php.net/manual/en/function.utf8-decode.php
  • $test = $preg_replace("/ÂÂ/",'"',$text); 不应在preg_replace 前面有$。现在尝试使用$test = preg_replace("/ÂÂ/",'"',$text); 并在echo$test; 中放置一个空格@ 没什么大不了的,但它确实将您的回声与您的变量echo $test; 分开了
  • $text = '这是一个listingTitle,有编码问题'; $test = utf8_decode($text);现在我明白了,这是一个有编码问题的列表标题
  • @NicolasRacine 然后你需要一个不同类型的函数并检查不在标准字符集中的字符。
  • @Fred -ii- 好的,所以我应该做的是检查每个字符,如果一个字符不在正确的字符集中,我会更改它吗?抱歉,我不太清楚你的意思

标签: php database encoding


【解决方案1】:

github中查看这个 PHP 类编码,其中包含流行的编码

用法:

$utf8_string = Encoding::toUTF8($utf8_or_latin1_or_mixed_string);

$latin1_string = Encoding::toLatin1($utf8_or_latin1_or_mixed_string);

还有:

$utf8_string = Encoding::fixUTF8($garbled_utf8_string);

例子:

echo Encoding::fixUTF8("Fédération Camerounaise de Football");
echo Encoding::fixUTF8("FÃédÃération Camerounaise de Football");
echo Encoding::fixUTF8("FÃÃédÃÃération Camerounaise de Football");
echo Encoding::fixUTF8("FÃÃÃédÃÃÃération Camerounaise de Football");

将输出:

Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football
Fédération Camerounaise de Football

更新:

检查这个:(我检查了这个工作)

$output = 'this is a listingTitle  with an encoding problem';
$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo($output);

输出:

this is a listingTitle with an encoding problem.

输出图片:

【讨论】:

  • Hum Html 净化器很酷!但它并没有消除编码错误。 :(
  • 1- u SET NAMES utf8 在插入数据之前? 2- 检查PhpMyadmin 并告诉我你的字符集是什么? 3- AddDefaultCharset UTF-8 在 Apache 配置中。
  • 现在我做了,(设置 UTF-8)但这不是问题,数据已经在数据库中,现在一切正常。但旧清单有这种错误。我的字符集是 utf-8_general_ci
  • 它和 :S 输出的字符串一样吗?而不是
  • 你救了我的命!
【解决方案2】:

之前我在从多种语言的xml 提要中获取数据时也遇到了同样的问题。

经过努力,我找到了一个解决方案,方法是使用utf8 编码创建所有内容。

1) 数据库联盟和字符集。

2) 数据库连接。

3) html 页面类型。

【讨论】:

  • 这是我一开始做的。但看起来这些错误在它自己的数据库中。
  • 是的,它不适用于数据库中已保存的内容。为此,您必须更新它们。但从现在开始,新的记录就可以了。
  • 是的,我知道,:) 但问题是我现在需要尝试修复那些在 :S 上有一些错误的旧列表
  • 不是 100% 确定,但在这种情况下,您必须先找到它们的实际编码,然后再翻译成它们的实际语言。
猜你喜欢
  • 2017-01-05
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 2023-04-02
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多