preg_match与UTF8

时间:2013-05-02 13:23:48

标签: php preg-match

我有一个字符串:

$string = 'This is Test';

还有一系列词语:

$array = array('test','example','blahblah');

我想查看$ string,看看是否有$ array中的任何单词:

$string_arr = explode(' ', $string);
foreach($string_arr as $value){
    if (preg_match("/\b$value\b/iu", $array))
        return true;
}

正如你所看到的,我使用了'u'标志来支持UTF-8,但有线的是它在我的wamp(localhost)上运行,但在我的真实服务器上的CentO上它不起作用,我google了,我发现了这个: http://chrisjean.com/2009/01/31/unicode-support-on-centos-52-with-php-and-pcre/

但是我无法访问服务器来升级RPM,所以我该怎么做呢?

提前致谢


任何人都可以提出另一种解决方案吗?我感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

我不确定你是否可以比这更简单:array_intersect($array,explode(' ',$string));你基本上只检查返回的数组是否有任何值,并且会告诉你$ array中的任何单词是否在$中串。以下是经过测试和运作的。

if( count(array_intersect($array,explode(' ',$string))) > 0 )
{
    echo 'We have a match!';
}

为了拥有完整的代码块...

$string = 'This is Test';
$array = array('test','example','blahblah');
$checked_array = array_intersect($array,explode(' ',$string));

if( count($checked_array) > 0)
{
    echo 'The following words matched: '.implode(', ',$checked_array);
}

答案 1 :(得分:0)

$testArray = explode(' ', $string);
foreach($testArray as $value){
    if(array_search($value, $testArray) !== false) return true;
}

答案 2 :(得分:0)

而不是使用preg_match使用array_search

$key = array_search($string, $array);
if(empty($key))
return true;
else
return false;