Skip to content

Instantly share code, notes, and snippets.

@petemcfarlane
Last active August 29, 2015 14:15
Show Gist options
  • Save petemcfarlane/a84f968a5356f9bfffeb to your computer and use it in GitHub Desktop.
Save petemcfarlane/a84f968a5356f9bfffeb to your computer and use it in GitHub Desktop.
Recursively converts short opening tags to standard ones for php files for a given directory
#!/usr/bin/env php
<?php
/*
* This script recursively converts short opening tags to standard ones for php files,
* given a directory as an argument
*/
$path = $argv[1];
if (!is_dir($path)) {
die("Error, $path is not a valid directory" . PHP_EOL);
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($iterator as $path => $file) {
if ($file->getExtension() == 'php') {
$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;
}
}
if (file_put_contents($file, $output)) {
echo $file . PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment