Created
July 1, 2013 12:54
-
-
Save nathggns/5900500 to your computer and use it in GitHub Desktop.
Convert Short Tags to open tags (Respect whitespace)
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 | |
define('DS', DIRECTORY_SEPARATOR); | |
/** | |
* Recursive glob | |
*/ | |
function recursive_glob($pattern, $flags = 0) { | |
$files = glob($pattern, $flags); | |
foreach (glob(dirname($pattern) . DS . '*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) { | |
$files = array_merge( | |
$files, | |
recursive_glob($dir . DS . basename($pattern), $flags) | |
); | |
} | |
return $files; | |
} | |
$files = array_merge(recursive_glob('./*.php'), recursive_glob('./*.ctp')); | |
// $files = array('./test.php'); | |
foreach ($files as $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; | |
$white = null; | |
preg_match('/^.*?([\s]+)$/', $code, $matches); | |
if (count($matches) > 0) { | |
$white = $matches[1]; | |
} | |
switch ($index) { | |
case T_OPEN_TAG_WITH_ECHO: | |
$output .= '<?php echo' . ($white ? $white : ' '); | |
break; | |
case T_OPEN_TAG: | |
$output .= '<?php' . ($white ? $white : ''); | |
break; | |
default: | |
$output .= $code; | |
break; | |
} | |
} else { | |
$output .= $token; | |
} | |
} | |
file_put_contents($file, $output); | |
echo '.'; | |
// die; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment