如何反转 preg_match_all php
How to invert preg_match_all php
如何反转 pre_match_all,所以下面代码的输出是 "you" 而不是 "hello" 和 "world"?
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
//preg_match
foreach ($text1 as $text) {
if (preg_match_all("~\b$text\b~i", $text2, $match)) {
$match = $match[0];
echo "<pre>";
print_r($match);
echo "</pre>";
}
}
您可以使用 str_ireplace,它是 str_replace.
的不区分大小写的版本
如果什么都不替换,剩下的就是 $text2 中没有的内容。
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
$text2 = explode(" ", $text2);
echo str_ireplace($text2, "", implode("",$text1));
// you
另一种方法是使用 array_diff 和 preg_grep。
Preg_grep 与正则表达式匹配 text2 中的单词,不区分大小写,returns 匹配的内容。
然后我使用 array_diff 查看与 $text1 和 preg_grep 的 return 有何不同。
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
$text2 = explode(" ", $text2);
Var_dump(array_diff($text1, preg_grep("/" . Implode("|", $text2) . "/i", $text1)));
您不需要 preg_match_all()
, preg_match()
就足够了(它 returns 1
如果它找到匹配项,0
否则)。
你也不需要匹配的子字符串,知道它是否在文本中找到一个词就足够了。
$words = array('you', 'hello', 'WORLD');
$text = "Hello world";
// Filter $words, keep only the items that are not present in $text
$missing = array_filter(
$words,
function($w) use ($text) {
// return TRUE when $w is not in $text
return preg_match('/\b'.preg_quote($w, '/').'\b/i', $text) == 0;
});
print_r($missing);
用非单词字符拆分字符串并使用 array_udiff
和 strcasecmp
作为回调:
$words = ['you', 'hello', 'WORLD'];
$str = "Hello world toto";
print_r(array_udiff($words, preg_split('~\W+~', $str), 'strcasecmp'));
// Array
// (
// [0] => you
// )
如何反转 pre_match_all,所以下面代码的输出是 "you" 而不是 "hello" 和 "world"?
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
//preg_match
foreach ($text1 as $text) {
if (preg_match_all("~\b$text\b~i", $text2, $match)) {
$match = $match[0];
echo "<pre>";
print_r($match);
echo "</pre>";
}
}
您可以使用 str_ireplace,它是 str_replace.
的不区分大小写的版本
如果什么都不替换,剩下的就是 $text2 中没有的内容。
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
$text2 = explode(" ", $text2);
echo str_ireplace($text2, "", implode("",$text1));
// you
另一种方法是使用 array_diff 和 preg_grep。
Preg_grep 与正则表达式匹配 text2 中的单词,不区分大小写,returns 匹配的内容。
然后我使用 array_diff 查看与 $text1 和 preg_grep 的 return 有何不同。
$text1 = array ('you', 'hello', 'WORLD');
$text2 = "Hello world";
$text2 = explode(" ", $text2);
Var_dump(array_diff($text1, preg_grep("/" . Implode("|", $text2) . "/i", $text1)));
您不需要 preg_match_all()
, preg_match()
就足够了(它 returns 1
如果它找到匹配项,0
否则)。
你也不需要匹配的子字符串,知道它是否在文本中找到一个词就足够了。
$words = array('you', 'hello', 'WORLD');
$text = "Hello world";
// Filter $words, keep only the items that are not present in $text
$missing = array_filter(
$words,
function($w) use ($text) {
// return TRUE when $w is not in $text
return preg_match('/\b'.preg_quote($w, '/').'\b/i', $text) == 0;
});
print_r($missing);
用非单词字符拆分字符串并使用 array_udiff
和 strcasecmp
作为回调:
$words = ['you', 'hello', 'WORLD'];
$str = "Hello world toto";
print_r(array_udiff($words, preg_split('~\W+~', $str), 'strcasecmp'));
// Array
// (
// [0] => you
// )