i would like to put a positive and a negative regular expressions in one preg_match(). Is that possible?
I use these if Condition.
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
}
}
And would like to simplify the code to only one preg_match ...something like these
if ( preg_match('/bot|crawler|spider|/^(?!/google|duckduck|yandex|baidu|yahoo|/).i', $user_agent) )
$post['url'] = $url;
}
But it is not working that way
If you want to ensure that the word 'bot' is present anywhere, and the word 'google' is not present anywhere (before or after 'bot'), you can use positive and negative lookaheads together.
if (preg_match('/^(?=.*(bot|crawler|spider))(?!.*(google|duckduck|yandex|baidu|yahoo)).*/i', $user_agent)) {