php字符串迭代不检测Π符号

时间:2015-01-20 09:13:12

标签: php string encoding utf-8 character-encoding

我在PHP“S * +%2YKΠ3)3Π+)+Θ”中有字符串,我想迭代。

我的代码:

$string = "S*+%2YKΠ3)3Π+)+Θ";
$charLength = mb_strlen($string);
for($i = 0 ; $i < $charLength ; $i++) {
    $char = mb_substr($string, $i , 1);
    if($char == 'Π') {
       echo "this is my Π character";
    }
}

但是我从来没有输入echo语句来得到“这是我的Π角色”,

有人帮我解决这个问题,请... 我已经在寻找其他答案,如:

How to convert any character encoding to UTF8 on PHP

Convert utf8-characters to iso-88591 and back in PHP

PHP: Convert any string to UTF-8 without knowing the original character set, or at least try

How to convert a string to utf-8 code in php

但从不为我的情况工作。谢谢

1 个答案:

答案 0 :(得分:0)

这样的代码很好,但是存在许多可能的缺陷:

  1. 你需要告诉mb函数他们应该使用什么编码。将编码单独传递给每个编码(例如mb_strlen($string, 'UTF-8')),或使用mb_internal_encoding为所有编码设置一次。
  2. 如果您希望字符串文字为UTF-8编码,则必须将您的.php源代码文件保存为UTF-8。检查您选择的文本编辑器,确实保存了编码为UTF-8(无BOM)的文件。