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.

5 thoughts on “Converting PHP scripts that still use « short-tags »

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">