Skip to content

Instantly share code, notes, and snippets.

@pokev25
Forked from jeff1326/shellcommand.sh
Created December 21, 2016 04:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pokev25/5923a6bd95b9f6fafbce4453e6a40dc2 to your computer and use it in GitHub Desktop.
Save pokev25/5923a6bd95b9f6fafbce4453e6a40dc2 to your computer and use it in GitHub Desktop.
script to parse PHP short open tags and convert ones to normal
find project/dir/ -type f -iname "*.php" -exec php -d short_open_tag=On the_script.php {} \;
<?php
$file = $argv[1];
echo "Replacing short open tags in \"$file\"\n";
$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 .= preg_replace('/<\?php\h+\n/', "<?php\n", preg_replace('/<\?php\h+/', '<?php ', str_replace(array('<?php', '<?'), array('<?', '<?php '), str_replace("\r\n", "\n", $code))));
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