Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created July 1, 2013 12:54
Show Gist options
  • Save nathggns/5900500 to your computer and use it in GitHub Desktop.
Save nathggns/5900500 to your computer and use it in GitHub Desktop.
Convert Short Tags to open tags (Respect whitespace)
<?php
define('DS', DIRECTORY_SEPARATOR);
/**
* Recursive glob
*/
function recursive_glob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern) . DS . '*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge(
$files,
recursive_glob($dir . DS . basename($pattern), $flags)
);
}
return $files;
}
$files = array_merge(recursive_glob('./*.php'), recursive_glob('./*.ctp'));
// $files = array('./test.php');
foreach ($files 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;
$white = null;
preg_match('/^.*?([\s]+)$/', $code, $matches);
if (count($matches) > 0) {
$white = $matches[1];
}
switch ($index) {
case T_OPEN_TAG_WITH_ECHO:
$output .= '<?php echo' . ($white ? $white : ' ');
break;
case T_OPEN_TAG:
$output .= '<?php' . ($white ? $white : '');
break;
default:
$output .= $code;
break;
}
} else {
$output .= $token;
}
}
file_put_contents($file, $output);
echo '.';
// die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment