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条)

报告相同问题?

悬赏问题

  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 求fpga交通信号灯设计Verilog代码
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头