2

I want extract the entire PHP Code of this section with Regular Expressions:

<h1>Extract the PHP Code</h1>
    <?php
        echo(date("F j, Y, g:i a") . ' and a stumbling block: ?>');
        /* Another stumbling block ?> */
        echo(' that works.');
    ?>
<p>Some HTML text ...</p>

Unfortunately, my Regular Expression got stuck on the stumbling block:

/<[?]php[^?>]*[?]>/gim

Does someone have a hint how to capture the full PHP Code?

2
  • 6
    If unversed with regex, use the php.net/tokenizer. Commented Aug 25, 2013 at 16:49
  • @Max are you performing this operation in your IDE / file editor, or are you intending to execute php code to modify your php code? Commented Feb 16, 2021 at 1:20

2 Answers 2

4

Something like this might work

/<\?php.+?\?>$/ms

Regular expression visualization

This pattern uses two flags

  • m for PCRE_MULTILINE

    By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl. When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

  • s for PCRE_DOTALL

    If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

Here's what a couple matches would look like

enter image description here


Caveat it doesn't work if it can't find ?> at the end of a line.

So it works in the case of

  • ?>');
  • ?> */

But it wouldn't work for

<?php
  echo "actual code";
  /*
   * comment ?>
   */
?>

Story short, if your code is that messy, you need a better solution. If your code is clean, it should work just fine.

Sign up to request clarification or add additional context in comments.

1 Comment

php does not need a tag closure.
3

You can try with this pattern:

$pattern = <<<'LOD'
~

#definitions
(?(DEFINE)
    (?<sq> '(?:[^'\\]+|\\.)*+' ) # content inside simple quotes
    (?<dq> "(?:[^"\\]+|\\.)*+" ) # content inside double quotes
    (?<vn> [a-zA-Z_]\w*+ ) # variable name
    (?<hndoc> <<< (["']?) (\g<vn>) \g{-2} \R # content inside here/nowdoc
              (?: [^\r\n]+ | \R+ (?!\g{-1}; $) )*+
              \R \g{-1}; \R
    )
    (?<cmt> /\*                      # multiline comments
             (?> [^*]+ | \* (?!/) )*+
             \*/
    )
)

#pattern
<\?php \s+
(?: [^"'?/<]+ | \?+(?!>) | \g<sq> | \g<dq> | \g<hndoc> | \g<cmt> | [</]+ )*+
(?: \?> | \z )

~xsm
LOD;

Test:

$subject = <<<'LOD'
<h1>Extract the PHP Code</h1>
    <?php
        echo(date("F j, Y, g:i a") . ' and a stumbling block: ?>');
        /* Another stumbling block ?> */
        echo <<<'EOD'
    Youpi!!! ?>
EOD;
        echo(' that works.');
    ?>
<p>Some HTML text ...</p>
LOD;

preg_match_all($pattern, $subject, $matches);

print_r($matches);

Another way:

As mario suggests it in a comment, you can use the tokenizer. It's the most easy way to do that since you don't have to define anything, example:

$tokens = token_get_all($subject);
$display = false;
foreach ($tokens as $token) {
    if (is_array($token)) {
        if ($token[0]==T_OPEN_TAG) $display = true;
        if ($display) echo $token[1];
        if ($token[0]==T_CLOSE_TAG) $display = false;
    } else {
        if ($display) echo $token;
    }
}

1 Comment

@m.buettner: single line comments don't stop ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.