Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created October 31, 2011 19:40
Show Gist options
  • Select an option

  • Save revolunet/1328628 to your computer and use it in GitHub Desktop.

Select an option

Save revolunet/1328628 to your computer and use it in GitHub Desktop.
tokenizer based php opening tags conversion
<?php
# use builtin PHP tokenizer to convert opening tags
# based on http://stackoverflow.com/questions/684587/batch-script-to-replace-php-short-open-tags-with-php/1647429#1647429
function convert($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;
}
}
return $output;
}
echo convert($argv[1]);
?>
#!/bin/sh
#
# shell script to convert old opening tags php file to new format
# usage example : find . -iname '*.php' -exec ./convert.sh {} \;
#
echo "converting $1"
tmp="$1.TMP"
php -n -f convert.php $1 > $tmp;
mv $tmp $1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment