Skip to content

Instantly share code, notes, and snippets.

@cgardner
Created April 21, 2012 02:08
Show Gist options
  • Select an option

  • Save cgardner/2433264 to your computer and use it in GitHub Desktop.

Select an option

Save cgardner/2433264 to your computer and use it in GitHub Desktop.

Revisions

  1. Craig Gardner created this gist Apr 21, 2012.
    21 changes: 21 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <?php

    $var = <<<VAR
    1
    VAR;

    // do stuff here
    echo $var;

    register_shutdown_function(function() {
    $contents = file_get_contents(__FILE__);
    $pattern = "\$var = <<<VAR\n%s\nVAR;";

    $search_pattern = '@\\'. sprintf($pattern, '(.*)') .'@m';
    preg_match($search_pattern, $contents, $matches);
    $new_contents = preg_replace_callback($search_pattern, function($matches) {
    return str_replace($matches[1], $matches[1] + 1, $matches[0]);
    }, $contents);

    file_put_contents(__FILE__, $new_contents, LOCK_EX);
    });