Created
January 29, 2019 16:07
-
-
Save ethanclevenger91/27b4daa907b8d033c35d59999490d964 to your computer and use it in GitHub Desktop.
Recursively and safely swap out PHP short open and short echo tags in a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Run like so: `php -d short_open_tag=On replace-short-open.php | |
* | |
* Shamelessly cobbled together from: | |
* https://stackoverflow.com/a/17161106/2233690 | |
* https://stackoverflow.com/questions/684587/batch-script-to-replace-php-short-open-tags-with-php/1647429#1647429 | |
* https://stackoverflow.com/a/684752/2233690 | |
* | |
*/ | |
/* | |
* | |
* Recursive glob | |
* | |
*/ | |
function rglob($pattern, $flags = 0) { | |
$files = glob($pattern, $flags); | |
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { | |
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags)); | |
} | |
return $files; | |
} | |
foreach (rglob("*.php") as $file) { | |
$content = file_get_contents($file); | |
$tokens = token_get_all($content); | |
$output = ''; | |
foreach($tokens as $token) { | |
if(is_array($token)) { | |
list($index, $code, $line) = $token; | |
switch($index) { | |
case T_OPEN_TAG_WITH_ECHO: | |
$output .= '<?php echo '; | |
break; | |
case T_OPEN_TAG: | |
$output .= '<?php '; | |
break; | |
default: | |
$output .= $code; | |
break; | |
} | |
} | |
else { | |
$output .= $token; | |
} | |
} | |
file_put_contents($file, $output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment