This "preg_replace" not work for me in php 7.4

1.2k Views Asked by At
$regex = array(
                "`^([\t\s]+)`ism"=>'',
                "`^\/\*(.+?)\*\/`ism"=>"",
                "`([\n\A;]+)\/\*(.+?)\*\/`ism"=>"$1",
                "`([\n\A;\s]+)//(.+?)[\n\r]`ism"=>"$1\n",
                "`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n"
            );
$file_content = preg_replace(array_keys($regex), $regex, $file_content);

I received this warning:

preg_replace(): Compilation failed: escape sequence is invalid in character class at offset 4

1

There are 1 best solutions below

2
On

The reason is \A inside square brackets.

Here is a good answer about the reason in general.

PHP PCRE engine migrates to PCRE2

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