【发布时间】:2020-09-30 16:39:39
【问题描述】:
我想把一个正则表达式和一个负正则表达式放在一个 preg_match() 中。这可能吗?
我使用这些 if 条件。
if ( preg_match('/bot|crawler|spider|/i', $user_agent) )
{
if ( preg_match('/google|duckduck|yandex|baidu|yahoo|/i', $user_agent) )
{
$post['url'] = $url;
}
else
{
// nothing
}
}
并且想将代码简化为只有一个 preg_match ...类似这些
if ( preg_match('/bot|crawler|spider|/^(?!/google|duckduck|yandex|baidu|yahoo|/).i', $user_agent) )
$post['url'] = $url;
}
但它不是那样工作的
【问题讨论】:
-
所以你试图匹配任何有
bot|crawler|spider的用户代理,在字符串的某个地方没有google|duckduck|yandex|baidu|yahoo?顺便说一句,您的原始 PHP 代码并没有这样做,它匹配两者而不是否定第二个。 -
嗨 BadeHorsie,是的,没错。我尝试匹配包含诸如“Bot”之类的单词但不包含诸如“Google”之类的单词的用户代理。
标签: php regex preg-match preg-match-all