Skip to content

Instantly share code, notes, and snippets.

@adduc
Created September 29, 2014 07:38
Show Gist options
  • Save adduc/388e2901e7b29ecda33c to your computer and use it in GitHub Desktop.
Save adduc/388e2901e7b29ecda33c to your computer and use it in GitHub Desktop.
Short open tag
<?php
/**
* find -name "*.php" -exec php -d short_open_tag=On replace-short-tags.php {} \
*/
if(!isset($argv[1])) {
fwrite(STDERR, 'ch');
exit(1);
}
$content = file_get_contents($argv[1]);
$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:
if(strpos($code, "<?php") === false) {
$space = substr($code, -1);
$space = trim($space) ? '' : $space;
$code = "<?php" . $space;
}
}
$output .= $code;
}
else {
$output .= $token;
}
}
file_put_contents($argv[1], $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment