dongluojiao6322 2014-01-23 15:05
浏览 108
已采纳

将.htaccess内容与PHP中的正则表达式进行匹配

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.

  • 写回答

2条回答 默认 最新

  • dongzhao3040 2014-01-23 15:24
    关注

    I believe you need s switch for DOTALL and preg_quote function to escape your variables for possible regex symbols.

    Try this regex:

    $regex_pattern = "/##bbc-start" . preg_quote($bot_banned_ip, '/') . 
                     preg_quote($bot_banned_mark, '/') . "(.*?)##bbc-end/is";
    

    Also do check your regex by doing:

    var_dump ( $regex_pattern );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站