我无法比较我认为应该完全相同的两个unicode字符。我怀疑它们的编码方式不同,但不知道如何将它们更改为相同的编码。
我要比较的字符来自缅甸Unicode块。我在php 5上运行wordpress,我试图制作一个自定义插件来处理缅甸Unicode。我所有的文件都是用UTF-8编码的,但是我不知道wordpress是做什么的。
我是这样做的:
function myFunction( $inputText ) {
$outputText = '';
$inputTextArray = str_split($inputText);
foreach($inputTextArray as $char) {
if ($char == "က") // U+1000, a character from the Myanmar Unicode block
$outputText .= $char;
}
return $outputText;
}
add_filter( 'the_content', 'myFunction');
在解决问题的这个阶段,该函数应该只返回它在内容中出现的地方。但是,除了空字符串之外,它永远不会返回任何东西,即使在帖子内容中明确显示了mail时也是如此。如果我将字符更改为任何拉丁字符,该函数将按预期工作。
所以,我的问题是,我如何编码这些字符($char
或"က"
),以便当$char
包含此字符时,它们比较相等。
str_split
不支持unicode。对于多字节字符,它将它们拆分为单个字符。尝试使用多字节字符串函数或preg_split
与/u
开关
$inputTextArray = preg_split("//u", $inputText, -1, PREG_SPLIT_NO_EMPTY);
http://codepad.viper - 7. - com/erfwcy
使用多字节函数mb_substr_count你也可以减少你的代码。像这样,function myFunction( $inputText ) {
return str_repeat("က", mb_substr_count($inputText, "က"));
}
或者使用正则表达式
preg_match_all("/က/u", $text, $match);
$output = implode("", $match[0]);