通过 REGEX 删除 CSS 评论时出错

error when removing CSS comments via REGEX

事实证明这两个序列(以前有效)

"`([\n\A;]+)\/\*(.+?)\*\/`ism" => "",     // error
"`([\n\A;\s]+)//(.+?)[\n\r]`ism" =>"\n",  // error

现在在PHP7.3

中抛出一个错误

警告:preg_replace():编译失败:转义序列在字符 class 偏移量 4

中无效

CONTEXT:考虑这个 snipit,它从字符串

中删除 CSS 评论
$buffer = ".selector {color:#fff; } /* some comment to remove*/";
$regex = array(
"`^([\t\s]+)`ism"=>'',
"`^\/\*(.+?)\*\/`ism"=>"",
"`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"",     // 7.3 error
"`([\n\A;\s]+)//(.+?)[\n\r]`ism"=>"\n", // 7.3 error
"`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n"
);
$buffer = preg_replace(array_keys($regex),$regex,$buffer);
//returns cleaned up $buffer value with pure css and no comments

参考:

Q1 - 在这种情况下,REGEX 有什么问题吗? 这个线程似乎暗示它只是一个放错位置的反斜杠 https://github.com/thujohn/twitter/issues/250

Q2 - 这是 PHP 7.3 错误还是此代码中的 REGEX 序列有问题?

不要在字符内使用零宽度断言 classes.

  • ^, $, \A, \b, \B, \Z, \z, \G - 作为锚点,(非)单词边界 - 在字符 class 中没有意义,因为它们不匹配任何字符。 ^\b 在字符 class 中的含义有所不同: ^ 是否定字符 class 标记,如果在 [ 之后使用或表示文字 ^\b 表示退格字符。

  • 你也不能在那里使用 \R(=任何换行符)。

字符 class 中带有 \A 的两个模式必须重写为 分组构造 (...),其中交替运算符 |:

"`(\A|[\n;]+)/\*.+?\*/`s"=>"", 
"`(\A|[;\s]+)//.+\R`"=>"\n", 

我删除了您不使用的多余修饰符和捕获组,并将 [\r\n] 替换为 \R"`(\A|[\n;]+)/\*.+?\*/`s"=>"" can also be re-written in a more efficient way:

"`(\A|[\n;]+)/\*[^*]*\*+(?:[^/*][^*]*\*+)*/`"=>""

请注意,在 PHP 7.3 中,符合。到 Upgrade history of the bundled PCRE library table, the regex library is PCRE 10.32. See PCRE to PCRE2 migration:

Until PHP 7.2, PHP used the 8.x versions of the legacy PCRE library, and from PHP 7.3, PHP will use PCRE2. Note that PCRE2 is considered to be a new library although it's based on and largely compatible with PCRE (8.x).

符合。到 this resource,更新后的库对正则表达式模式更加严格,现在将以前宽容接受的用户错误视为真正的错误:

  • Modifier S is now on by default. PCRE does some extra optimization.
  • Option X is disabled by default. It makes PCRE do more syntax validation than before.
  • Unicode 10 is used, while it was Unicode 7. This means more emojis, more characters, and more sets. Unicode regex may be impacted.
  • Some invalid patterns may be impacted.

In simple words, PCRE2 is more strict in the pattern validations, so after the upgrade, some of your existing patterns could not compile anymore.