Skip to content

Instantly share code, notes, and snippets.

@jeff1326
Forked from anonymous/shellcommand.sh
Last active October 28, 2019 11:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeff1326/5bbaae9b6a728c8f99fd67297806dd07 to your computer and use it in GitHub Desktop.
Save jeff1326/5bbaae9b6a728c8f99fd67297806dd07 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);
@jeff1326
Copy link
Author

jeff1326 commented Aug 3, 2016

I added new line for ouptut and improved T_OPEN_TAG.
I tested it on a upgrade of a project from php 5.3 to php 5.6.

@jeff1326
Copy link
Author

jeff1326 commented Aug 3, 2016

The command i used in my case since this was a not clean project :

find . -type f \( -iname "*.php" -o -iname "*.tpl" -o -iname "*.css" -o -iname "*.inc" -o -iname "*.js" \) -exec php -d short_open_tag=On the_script.php {} \;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment