phpregexparsingmultilinephp-parser

Regex to match a result that isn't single line and expanded across multiple lines


I want to change /<\?php\s([\s\S]*?)\?>/gi the way that single line PHP tags become excluded.

For example, here I want to match only second PHP tag and not the first one:

Test number <?PHP echo($a);?> is here.

This is test number <?PHP echo($b);
$b = $a?> that expanded across multiple lines.

Solution

  • You can use

    <\?php(?!\S)((?:(?!<\?php(?!\S)|\?>).)*\R[\s\S]*?)\?>
    

    A variation with multiline .:

    <\?php\s((?:(?!<\?php\s|\?>).)*\R(?s:.*?))\?>
    

    See the regex demo. Details: