PHP str_replace删除无意中删除中文字符

时间:2017-11-20 16:30:15

标签: php utf-8 str-replace chinese-locale

我有一个删除特殊字符的PHP脚本,但不幸的是,一些中文字符也被删除了。

If Range("B4").Value = 1 Then Range("C4").Value = 0
奇怪的是,输出是<?php function removeSpecialCharactersFromString($inputString){ $inputString = str_replace(str_split('#/\\:*?\"<>|[]\'_+(),{}’! &'), "", $inputString); return $inputString; } $test = '赵景然 赵景然'; print(removeSpecialCharactersFromString($test)); ?> 。角色赵然 赵然已删除

此外,也会被删除。可能的原因是什么?

1 个答案:

答案 0 :(得分:1)

您用来作为要替换的内容列表的字符串与混合编码不兼容。我所做的是将此字符串转换为UTF16,然后将其拆分。

parent.elemento.style.position = relative; 
this.elemento.style.position = absolute;

这给了......

function removeSpecialCharactersFromString($inputString){
    $inputString = str_replace(str_split(
            mb_convert_encoding('#/\\:*?\"<>|[]\'_+(),{}’! &', 'UTF16')), "", $inputString);
    return $inputString;
}
$test = '#赵景然 赵景然';
print(removeSpecialCharactersFromString($test));

BTW - 赵景然赵景然 是MB安全的 - 有点认可海报...... http://php.net/manual/en/ref.mbstring.php#109937