-
-
Save jeff1326/5bbaae9b6a728c8f99fd67297806dd07 to your computer and use it in GitHub Desktop.
script to parse PHP short open tags and convert ones to normal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find project/dir/ -type f -iname "*.php" -exec php -d short_open_tag=On the_script.php {} \; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
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
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.