I'm trying to regex
match the couple of things from the content of .htaccess
file.
Basically everything in between the comments
##bbc-startSOME_IP_HERE_AND_SOME_LABEL
and ##bbc-end
The idea is to grab the rule between comments and replace it with empty string, if exists:
<IfModule mod_rewrite.c>
RewriteEngine On
##bbc-start-1Mail.RU_Bot
RewriteCond %{REMOTE_HOST} -1 [OR]
RewriteCond %{HTTP_USER_AGENT} Mail.RU_Bot
RewriteRule . - [F]
##bbc-end
##bbc-start66.249.78.150googlebot
RewriteCond %{REMOTE_HOST} 66.249.78.150 [OR]
RewriteCond %{HTTP_USER_AGENT} googlebot
RewriteRule . - [F]
##bbc-end
##bbc-start157.55.33.50bingbot
RewriteCond %{REMOTE_HOST} 157.55.33.50 [OR]
RewriteCond %{HTTP_USER_AGENT} bingbot
RewriteRule . - [F]
##bbc-end
</IfModule>
Used pattern is:
$regex_pattern = "/##bbc-start{$bot_banned_ip}{$bot_banned_mark}(.*?)##bbc-end/m";
...where {$bot_banned_ip}
and {$bot_banned_mark}
are provided in runtime.
The above pattern was tested and works just fine on-line on http://www.rubular.com/r/bfoeQnah49
and on http://www.regexe.com/, but surprisingly fail to work in PHP environment, 5.3.18 with preg_match()
or preg_replace()
.
Do I miss something, is it the problem with my environment?
EDIT: The exact code is:
$file_contents = '<IfModule mod_rewrite.c>
RewriteEngine On
##bbc-start-1Mail.RU_Bot
RewriteCond %{REMOTE_HOST} -1 [OR]
RewriteCond %{HTTP_USER_AGENT} Mail.RU_Bot
RewriteRule . - [F]
##bbc-end
##bbc-start66.249.78.150googlebot
RewriteCond %{REMOTE_HOST} 66.249.78.150 [OR]
RewriteCond %{HTTP_USER_AGENT} googlebot
RewriteRule . - [F]
##bbc-end
##bbc-start157.55.33.50bingbot
RewriteCond %{REMOTE_HOST} 157.55.33.50 [OR]
RewriteCond %{HTTP_USER_AGENT} bingbot
RewriteRule . - [F]
##bbc-end
</IfModule>';
$regex_pattern = "/##bbc-start{$bot_banned_ip}{$bot_banned_mark}(.*?)##bbc-end/m";
$match = preg_match($regex_pattern, $file_contents);
//preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
if ($match===1) { /*DO SOME STUFF*/}
The strange thing is that $match
is always 0.