0

我有一个字符串:

$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),但在我的 CentOs 上的真实服务器上它不起作用,我用谷歌搜索并发现了这个: http://chrisjean.com/2009/01/31/unicode-support-on-centos-52-with-php-and-pcre/

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

提前致谢


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

4

3 回答 3

1

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

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);
}
于 2013-05-02T13:36:14.177 回答
0

而不是使用 preg_match 使用 array_search

$key = array_search($string, $array);
if(empty($key))
return true;
else
return false;
于 2013-05-02T13:30:45.927 回答
0
$testArray = explode(' ', $string);
foreach($testArray as $value){
    if(array_search($value, $testArray) !== false) return true;
}
于 2013-05-02T13:29:40.990 回答