$string = 'operating-system';
$array = array('operating-system');
$i = contains($array, $string);
echo ($i) ? "found ($i)" : "not found";
以上代码打印 found(1)
$string = '운영체제';
$array = array('운영체제');
$i = contains($array, $string);
echo ($i) ? "found ($i)" : "not found";
但是此代码会打印 not found 。为什么?
我更新了charset = utf-8
function contains($needles, $haystack) {
return count(array_intersect($needles, explode(" ", preg_replace("/[^A-Za-z0-9' -]/", "", $haystack))));
}
答案 0 :(得分:1)
您需要一个支持多字节字符的本机函数。您可以使用mb_ereg_replace。
代替preg_replace
function contains($needles, $haystack) {
return count(array_intersect($needles, explode(" ", mb_ereg_replace("/[^A-Za-z0-9' -]/", "", $haystack))));
}