如何比较preg_match_all不区分大小写的数组

时间:2018-07-18 19:33:45

标签: php arrays preg-match-all

我已经使用array_udiff()来比较不区分大小写的数组,这在下面的代码中可以正常工作,即在两个数组中都发现单词"flower"不管是小写还是大写。

$array1 = array("sun", "World", "FLOWER");
$array2 = array("you", "your", "flower");

$result = array_udiff($array1, $array2, 'strcasecmp');

print_r($result);

输出:

Array ( [0] => sun [1] => World )

现在我一直在尝试将array_udiff()preg_match_all()一起使用。首先看下面带有array_diff()的示例。第一个preg_match_all()在数组$findWords$text1之间寻找匹配项。然后,第二个preg_match_all()在第一个preg_match_all()$text2的输出中寻找匹配项。

$text1 = "Hello WORLD";
$findWord = array('WORLD', 'you');

// First preg_match
foreach ($findWord as $findWords1) {
    if (preg_match_all("~\b$findWords1\b~i", $text1, $matchWords1)) {  
        $matchWords1 = $matchWords1[0];
    }  

    $text2 = "Hello world";

    // Second preg_match
    foreach ($matchWords1 as $findWords2)  
        if (preg_match_all("~\b$findWords2\b~i", $text2, $matchWords2)) {
            $matchWords2 = $matchWords2[0];
        }    

    $result = array_diff($matchWords1, $matchWords2);
    $match = array_unique($result);

    foreach ($match as $matches) 
        echo $matches.'<br>';  
}

输出:

WORLD

所以这是我的问题,我希望能够比较两个数组$matchWords1$matchWords2,因此,如果$text2中的“ world”是小写字母,则仍然与“世界”匹配。请注意,我想保留$findWords中的大写/小写单词,因此我不需要寻找strtolower()的解决方案。

这是我尝试使用array_udiff()strcasecmp()的情况,但是它返回一个空数组:

$text1 = "Hello WORLD";
$findWord = array('WORLD', 'you');

// First preg_match
foreach ($findWord as $findWords1) {
    if (preg_match_all("~\b$findWords1\b~i", $text1, $matchWords1)) {  
        $matchWords1 = $matchWords1[0];
    }  

    $text2 = "Hello world";

    // Second preg_match
    foreach ($matchWords1 as $findWords2)  
        if (preg_match_all("~\b$findWords2\b~i", $text2, $matchWords2)) {
            $matchWords2 = $matchWords2[0];
        }    

    $result = array_udiff($matchWords1, $matchWords2, 'strcasecmp');
    $match = array_unique($result);

    echo "<pre>"; 
    print_r($match); 
    echo "</pre>";  
}

输出:

Array ( ) Array ( )

编辑:

以下代码的输出是单词“ WORLD ”,但是我想使该代码不区分大小写,因此没有输出。例如,如果将$ text2中的“ world”更改为UPPERCASE,将不会有任何输出,这就是我想要的结果,但是$ text2中的单词“ world”为小写。因为“世界”和“世界”是同一个词。

$text1 = "Hello WORLD";

    $findWord = array('WORLD', 'you');

    // First preg_match
    foreach ($findWord as $findWords1)            {

    if (preg_match_all("~\b$findWords1\b~i", $text1, $matchWords1)) {  

    $matchWords1 = $matchWords1[0];

    }  

    $text2 = "Hello world";

    // Second preg_match
    foreach ($matchWords1 as $findWords2)  

    if (preg_match_all("~\b$findWords2\b~i", $text2, $matchWords2))  {

        $matchWords2 = $matchWords2[0];

    }    

    $result = array_diff($matchWords1, $matchWords2);
    $match = array_unique($result);

    foreach ($match as $matches) 

    echo $matches.'<br>';
}

1 个答案:

答案 0 :(得分:0)

array_udiff返回第二个数组中不存在的第一个数组元素。如果需要相反的操作,则需要使用array_uintersect。只需更改行:

$result = array_udiff($matchWords1, $matchWords2, 'strcasecmp');

收件人:

$result = array_uintersect($matchWords1, $matchWords2, 'strcasecmp');

它应该可以工作。