Converting PHP scripts that still use « short-tags »

This is a translation of an older article (original in French).

If you kept bad habits or you just got some old PHP scripts that were still using PHP short tags (i.e. (<? ?> instead of <?php ?>) which are disabled by default in current PHP versions, the following script (shorttags.sh) is for you:

#!/bin/sh
find "$@" -name "*.php" -exec perl -i -wpe 's/<\?=/<?php echo /g' {} \; \
-exec perl -i -wpe 's/<\?(?!php|xml)/<?php /g' {} \;

After downloading this script, make it executable:

chmod +x shorttags.sh

Then you can use it like this (don’t forget to make a backup of your precious scripts, you never know…):

./shorttags.sh /path/to/your/php/files/

Note that you can specify more than one path from this command line.