Skip to content

Fixing PHP short open tags

Find and replace in Eclipse PDTA PHP block is started with the tag <?php. However, there is also an alternative known as a "short open tag" which is to only use <?

The problem with using short open tags is that they conflict with xml parsers and for that reason, support for short open tags has to be enabled. By default, it's disabled and enabling deprecated features may be impossible if hosts don't allow it.

Every so often you may come upon a code base that was written using short open tags.

Often people are first confronted with this problem when they have a legacy code base, and either move it to a new server or upgrade php only to find that the site is spewing errors and no longer functional. In a worse case scenario portions of your php code will be plainly visible to end users due to the fact that the php parser is no longer parsing those blocks and simply returning them as html text.

There are a number of different approaches you can take to solve this problem. First you can turn on the support for short open tags, but as I mentioned previously, this is not recommended.

Secondarily, there is at least one tool available with the name of phptags tag tidier that will parse and edit your source files. From the looks of it, it's an open source, php based package that uses some scripting to make it into a *nix command line utility. It looks pretty solid and seems to be maintained and relatively current, but I haven't tried it out personally.

If you are already having to do a lot of other edits, aren't finding the tool compatible, or just aren't entirely comfortable trusting a tool not to mangle your source files, there is always the search and replace feature in your code editor.

Without regular expression support in your editor, this is likely to be a slow and error-prone process, especially if the short open tags are intermixed with the normal tags, or the allowable alternative syntax tags like <?= You'll also be likely to screw up your formatting, requiring you to fix things manually.

A regex Search and replace solution



This simple search and replace pair should do the trick:

Find:
(<\?)(\s)

Replace:
<?php$2


Without going too much into regex, this utilizes 2 capturing groups, which is why I used parens around the two sections. Inside the first capturing group (), you actually find the short tag with <\?. A question mark is a special regex character which is why in regex, when you need to find a literal question mark, you have to escape it first with the backslash.

So as not to also find <?php or <?=, you only want to match <? when it is followed by whitespace.

The 2nd capturing group uses a built in regex character class (\s) to match any existing whitespace. You need to do this because it is not uncommon for a block to be immediately followed by a carriage return, however it might also be followed by a space if it's a small inline php block intermixed within some html code.

Here's 2 examples:


<?
   // Here's a big block of php coming
?>
 


vs.


User: <? if ($user) echo $user ?> Date: <? echo $date ?>
 


The reason we capture the whitespace in its own group, is that when we replace the short open tag, we want to leave the whitespace alone. That way you aren't going to screw up the original formatting that existed before your replacement.

In the replacement we change our <? to <?php, but then immediately restore whatever whitespace we originally matched using the $2

Double check the regex replacement syntax within your editor's documentation. The way you specify a captured portion in a replacement may be different, but just about all text editors that support regex also seem to support this capability in some fashion.

Using your editor's search and replace will allow you to test out this regex and gain comfort, or even step through the replacements, and of course if something goes horribly wrong, you will be able to undo those changes. Once you're comfortable with the stability of this, replacement of short open tags in legacy code should be a simple and relatively painless operation.
Defined tags for this entry: , , , ,

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Pavatar, Gravatar, Favatar, MyBlogLog, Pavatar author images supported.
BBCode format allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Form options