您好了以下问题
首先:如果ÄÖÜ提供正则表达式,则商标符号替换正在生成附加字符:。:
第二:如果我在结果中进行字符串循环,则所有特殊字符都是 。
问题是,为什么会发生这种情况,我该怎么办呢? (第二个问题不是那么令人讨厌但很有趣)
header('Content-Type: text/html; charset=utf-8');
$testtxt = 'MicrÖsüft W!ndows® is a trÄdemark of Microfrost™ ©2012!';
$r = preg_replace('#[^\w\s\däöüß%\!\?\.,\:\-_\[\]ÄÖÜ]#is', 'X', $testtxt);
echo $testtxt, '<br>', $r;
echo '<hr>';
for($i = 0, $size = strlen($r); $i < $size; ++$i) {
echo $r[$i], '=', ord($r[$i]), '<br>';
}
结果:
MicrÖsüft W!ndows® is a trÄdemark of Microfrost™ ©2012!
MicrÖsüft W!ndowsXX is a trÄdemark of MicrofrostX�X XX2012!
M=77
i=105
c=99
r=114
�=195
�=150....
预期:
MicrÖsüft W!ndows® is a trÄdemark of Microfrost™ ©2012!
MicrÖsüft W!ndowsXX is a trÄdemark of MicrofrostXX XX2012!
M=77
i=105
c=99
r=114
Ö=195
s=150....
答案 0 :(得分:2)
您正在使用与多字节字符不兼容的strlen和ord函数。 以下代码应显示每个字符的字节数:
for($i = 0, $size = mb_strlen($r); $i < $size; ++$i) {
echo $r[$i], '=', strlen($r[$i]), '<br>';
}
其次,您应该将UTF-8修饰符添加到正则表达式中:
$r = preg_replace('#[^\w\s\däöüß%\!\?\.,\:\-_\[\]ÄÖÜ]#isu', 'X', $testtxt)
答案 1 :(得分:0)
我不太确定,但您可以尝试在preg_replace()中将“u”修饰符添加到正则表达式中。或者尝试使用mb_eregi_replace()。
另外,你确定可以使用ord()吗?在手册中写入它返回 ASCII 值(我强烈怀疑,这意味着对于多字节字符,它将返回第一个字节的值)。