Created
October 31, 2011 19:40
-
-
Save revolunet/1328628 to your computer and use it in GitHub Desktop.
tokenizer based php opening tags conversion
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 | |
| # 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]); | |
| ?> |
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
| #!/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