Using Sed to Update PHP Short tags

Why?

There are a couple of Stackoverflow discussions of what constitutes PHP shorthand, and why you would possibly not want to use them.

For me, it boils down to portability. It is fairly easy to enable shorttags in the php.ini file by setting

short_open_tag=On

However, I’d just as soon reduce the troubleshooting that I have to do if I ever move to a new environment. This happened recently as I was importing an old WordPress site into my Varying Vagrant Vagrants machine (which has shorttags disabled by default).

Using Sed

I could have used search and replace inside of my editor, however, why not use the power of Sed to quickly replace the tags? Taken even further – why not create a bash script to iterate over all PHP files in a directory?

The Sed command

sed 's:<?=:<?php echo:g' index.php | sed 's:<?:<?php:g' | sed 's:<?phpphp:<?php:g'

At the heart of this is three sed ‘substitution’ commands, piped together.

The first replaces the echo short tag <?= with the verbose <?php echo. The next pipe replaces the basic opening short tag, while the last pipe reverts any tags that originally started out correctly, but were garbled by the second sed statement.

Also note the g at the end of each sed statement. This is the global replacement flag. This is necessary to ensure that sed will operate on all matches within a line of text, not just the first match.

But it Doesn’t Update the File

So the above code is good, there is only one problem, it only pipes the output to stdout. Additionally if you try and use our friend the greater than sign to pipe it into the original file – ‘index.php’ in the above example, it will completely wipe out that file. Yikes!

Of the listed solutions in the above stackoverflow thread, I particularly like writing the contents of the pipe to a temporary file, and then moving the temporary file to the original file’s location. So our new command, which now writes the changes to the original file, looks like:

sed 's:<?=:<?php echo:g' index.php | sed 's:<?:<?php:g' | sed 's:<?phpphp:<?php:g' > index.php.tmp && mv index.php.tmp index.php

Script It!

And finally, because it would be tedious to write out that command for every file that we want to update, lets make a script file that will run our sed command for every php file:

#! /bin/bash
for f in *.php
do
echo "Converting $f"
sed 's:<?=:<?php echo:g' $f | sed 's:<?:<?php:g' | sed 's:<?phpphp:<?php:g' > $f.tmp && mv $f.tmp $f
done

Just create a new file, copy the above code to that file, make it executable, and run it!


Posted

in

by

Tags:

Comments

3 responses to “Using Sed to Update PHP Short tags”

  1. Maarten Avatar
    Maarten

    This will run your sed along the whole directory structure :).

  2. Maarten Avatar
    Maarten

    #! /bin/bash
    find . -type f -iname “*.php” -print0 | while IFS= read -r -d $” f; do
    echo “Converting $f”
    sed ‘s:<?=:<?php echo:g' $f | sed 's:<?:<?php:g' | sed 's:<?phpphp: $f.tmp && mv $f.tmp $f
    done

  3. Benjamin Avatar

    Cool Maarten – thanks for improving!