php数组包含不支持多语言

时间:2016-04-07 23:24:45

标签: php arrays utf-8 contains

$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))));
    }

1 个答案:

答案 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))));
}

您可能还想查看docs for all multibyte string functions