Last active
September 30, 2018 09:42
-
-
Save mytory/5380b6a970ed4c14e16dd1be498e0919 to your computer and use it in GitHub Desktop.
Change short open tag to long open tag.
This file contains 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 | |
// This script convert short open tag to long open tag. | |
// Inspired from https://stackoverflow.com/a/3621669 | |
// | |
// Source: https://gist.github.com/mytory/5380b6a970ed4c14e16dd1be498e0919 | |
// Usage: php short-open-tag-converter.php target_file_path.php | |
// Notice: This script change original file. | |
// | |
// If you want to change '<?=' to '<?php echo ' uncomment line 45~50. | |
if (ini_get('short_open_tag') !== '1') { | |
fwrite(STDERR, 'ERROR! Change short_open_tag to On in ' . php_ini_loaded_file() . PHP_EOL); | |
exit(1); | |
} | |
if ($argc === 1) { | |
fwrite(STDERR, "ERROR! A file path must be entered." . PHP_EOL); | |
fwrite(STDERR, "ex) php {$argv[0]} file_path.php" . PHP_EOL); | |
exit(1); | |
} | |
$filepath = realpath($argv[1]); | |
$contents = file_get_contents($filepath) or die; | |
$tokens = token_get_all($contents); | |
$tokens[] = array(0x7E0F7E0F,"",-1); | |
$fixed_contents = ''; | |
foreach ($tokens as $index => $token) { | |
if (is_array($token)) { | |
list($toktype, $source_code) = $token; | |
if ($toktype == T_OPEN_TAG) { | |
if (($source_code == "<?") && ($tokens[$index+1][0] != T_STRING)) { | |
$source_code = "<?php"; | |
if ($tokens[$index+1][0] != T_WHITESPACE) { | |
$source_code .= " "; | |
} | |
} | |
} | |
// <?= to <?php echo | |
// if ($toktype == T_OPEN_TAG_WITH_ECHO) { | |
// $source_code = "<?php echo"; | |
// if ($tokens[$index+1][0] != T_WHITESPACE) { | |
// $source_code .= " "; | |
// } | |
// } | |
$fixed_contents .= $source_code; | |
} else { | |
$fixed_contents .= $token; | |
} | |
} | |
file_put_contents($filepath, $fixed_contents); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment