Skip to content

Instantly share code, notes, and snippets.

@ernestom
Forked from panquetofobia/shortags_fixer.php
Created August 21, 2012 18:53
Show Gist options
  • Save ernestom/3418313 to your computer and use it in GitHub Desktop.
Save ernestom/3418313 to your computer and use it in GitHub Desktop.
PHP short open tags fixer
<?php
/**
* Short Open Tags fixer.
*
* Usage:
* $ php -d short_open_tag=On -f shortags_fixer.php path/to/file.php
* $ for i in $(find ./ -iname "*.phtml"); do php -d short_open_tag=On -f shortags_fixer.php $i > ~/fixed.txt && cat ~/fixed.txt > $i; done
*/
function fix($file)
{
if (!file_exists($file)) {
return;
}
$tokens = token_get_all(file_get_contents($file));
$output = '';
foreach($tokens as $token) {
if (!is_array($token)) {
$output .= $token;
continue;
}
list($index, $code) = $token;
$convertions = array(
T_OPEN_TAG => '<?php ',
T_OPEN_TAG_WITH_ECHO => '<?php echo '
);
$output .= isset($convertions[$index]) ? $convertions[$index] : $code;
}
return $output;
}
if (isset($argv[1])) {
echo fix($argv[1]);
} else {
echo "Usage: php -d short_open_tag=On -f shortags_fixer.php path/to/file.php\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment