【问题标题】:Regex working in RegExr, doesn't work in PHP正则表达式在 RegExr 中工作,在 PHP 中不起作用
【发布时间】:2012-08-10 03:28:31
【问题描述】:

我在 RegExr 中编写了一个正则表达式来处理以下字符串:

<?php _on*/4353452f43f43f46 xx46 _off*/ ?>

这是正则表达式代码:

(.*<?php.*)(.*_on.*)(.*_off.*)(.*?>)

这里运行良好:

http://regexr.com?31ptt

但它不适用于 PHP,我收到奇怪的错误,例如:“未知修饰符 '

我需要做什么才能将其转换为与 PHP 一起使用?

这是我的 php 代码:

$virusstring = '(.*/<?php.*)(.*_on.*)(.*_off.*)(.*?>)';    
if(preg_match($virusstring,$myfile)) {
    $fixed = preg_replace($virusstring,'',$myfile);
    $blah = file_put_contents($item, $fixed);
}

$myfile 只是从正在扫描的受感染文件中获取的。

【问题讨论】:

  • 向我们展示您尝试使用的 PHP 代码。
  • 如果您显示您的 PHP 代码,这将更容易排除故障。
  • PHP 使用 PCRE(与 PREG 兼容),也许您正在使用的 RegExr 不使用
  • 尝试使用 this tester 并选择 PREG。你会看到你的 RegEx 失败了。

标签: php regex


【解决方案1】:

您的正则表达式缺少分隔符。您需要添加分隔符,否则 PHP 会假定您的开头 ( 是分隔符:

/(.*<\?php.*)(.*_on.*)(.*_off.*)(.*\?>)/

另外,? 是一个量词,匹配前一个字符的 0 或 1。你需要逃避它:

(.*<\?php.*)(.*_on.*)(.*_off.*)(.*\?>)

【讨论】:

    【解决方案2】:

    这似乎工作正常

    preg_match("/(.*<\\?php.*)(.*_on.*)(.*_off.*)(.*\\?>)/us", $searchText)
    

    【讨论】:

      猜你喜欢
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 2016-08-01
      相关资源
      最近更新 更多