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.

  • Share/Bookmark

Tags: , ,

5 commentaires pour “Converting PHP scripts that still use « short-tags »”

  1. Frank Groeneveld dit :

    Are you sure the <?= tag is also disabled? I believe that tag is still valid…

    reply/répondre

    jernst Reply:

    @Frank Groeneveld
    I just tried it on my server and <?= doesn’t work here when short tags are disabled.

    reply/répondre

  2. Frank Groeneveld dit :

    @jernst
    Ok, I’ll have to convert my code than also :)

    reply/répondre

  3. Kevin van Zonneveld dit :

    I think this will also convert:
    <?xml

    to
    <?php xml

    You could do something like this though:
    <\?(?!php|xml)

    I wrote an article about it here
    http://kevin.vanzonneveld.net/techblog/article/prepare_for_php_53/

    reply/répondre

    jernst Reply:

    @Kevin van Zonneveld
    You’re right! Thank you for your comment!

    I’ve updated the regex accordingly.

    reply/répondre

Laisser un commentaire