Replacing short tags with proper PHP tags
By Geoffrey on Thursday 29 January 2009, 10:37 - Coding - Permalink
This is a little script I made to get rid of those damned short tags.
[php]
<?php
while ($file = trim(fgets(STDIN)))
{
$content = file_get_contents($file);
$search = array('/<\?=/', '/<\?(?!php|xml)/');
$replace = array('<?php echo ', '<?php ');
if ($content != ($new_content = preg_replace($search, $replace, $content)))
{
file_put_contents($file, $new_content);
}
}
Just put this in a file, short_tags.php for example, and run something like:
$ find . -name "*.php" | php ./short_tags.php
I would have done it with sed, but it doesn't seem to support PCRE, and I don't know how to do negative lookahead (the (?!php|xml) thingy) with POSIX based regexp (if it's even supported) :/
UPDATE: Actually, POSIX Regexps DO support negative lookahead (as well as positive lookahead and lookbehind) with the same syntax as PCRE regexps. But grep doesn't use POSIX regexps, it uses things called BRE and ERE that I've never heard of before. Too bad.
Comments
Thanks, it helped me :)