Skip to content

Instantly share code, notes, and snippets.

@mcascardi
Forked from jankkhvej/shellcommand.sh
Last active October 27, 2021 19:33
Show Gist options
  • Save mcascardi/695b349424ea63e906949130f60763b8 to your computer and use it in GitHub Desktop.
Save mcascardi/695b349424ea63e906949130f60763b8 to your computer and use it in GitHub Desktop.
Descend into a directory, parse code for short open tags and convert them to normal
#!/bin/bash
find $1 -type f -iname "*.php" -exec php -d short_open_tag=On ./convert.php {} \;
<?php
$file=$argv[1];
echo "Replacing short open tags in \"$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;
}
}
file_put_contents($file, $output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment