php 字符串迭代未检测到 Π 符号

php String iteration does not detect Π symbol

我在 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";
    }
}

但是我从来没有输入回显语句来得到"this is my Π character",

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

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. 您需要告诉 mb 函数它们应该使用什么编码。要么将编码分别传递给每个人(例如 mb_strlen($string, 'UTF-8')),要么使用 mb_internal_encoding.
  2. 为所有人设置一次编码
  3. 如果您希望字符串文字采用 UTF-8 编码,则您的 .php 源代码文件必须以 UTF-8 格式保存。检查您选择的文本编辑器,您实际上保存的文件编码为 UTF-8(无 BOM)。