【问题标题】:How to compare preg_match_all case-insensitive arrays如何比较 preg_match_all 不区分大小写的数组
【发布时间】:2018-07-18 19:33:44
【问题描述】:

我使用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”是小写的,它仍然匹配“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>';
}

【问题讨论】:

  • 太多了,只显示输入,输出应该是什么以及为什么输出应该是那个。
  • 让问题更具体。应该是这样的:输入 - 输出 - 首选输出 - 你有问题的代码。简单但有用:)
  • 请参阅minimal reproducible example,并专注于明确的问题陈述(和“最小”)方面。
  • array_udiff() 返回两个数组中not 的单词。但是你想在两个数组中找到 的单词,正好相反。
  • 使用array_uintersect()

标签: php arrays preg-match-all


【解决方案1】:

array_udiff 返回第一个数组中第二个数组中不存在的元素。如果你想要相反,你需要使用array_uintersect。换行就好了:

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

到:

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

它应该可以工作。

【讨论】:

  • 感谢您的回答。但 array_uintersect 仍然返回“世界”。如果单词“world”在 $findWord 以及 $text1 和 $text2 中,则不应有任何输出。如果在 $text2 中将“世界”一词大写,则没有任何输出,这就是我想要的,但不区分大小写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多