Web Hosting Talk







View Full Version : fix short opening tag - php


orbitz
04-23-2008, 05:23 PM
Do you know any shell or other script that can replace all php script using the short opening tag <? with <?php

Thanks!

blueroomhosting
04-23-2008, 05:44 PM
What OS?

Under any reasonable unix system it would be something like this (but please make backups and experiment first):

find . -name "*.php" | xargs sed -i -e 's/<[?]$/<?php/' -e 's/<[?]\([ \t]\)/<?php\1/g'


If you are lucky then your sed supports the "-i" flag that tells it to modify files in place. The first expressions matches lines ending in "<?" and the second matches any occurrence of "<?" followed by a space or a tab. There might be a way of combining the two into one expression but it doesn't really matter.

Jim

orbitz
04-23-2008, 05:47 PM
Thanks blueroomhosting. This is on Linux (Debian).

The server is shared by many users; thus, many scripts were created by different users. Would this cause some changes to the owner and the permission setting of the scripts?

Thanks!

blueroomhosting
04-23-2008, 05:56 PM
Debian's sed should have "-i" support.

You would need to run that as root to be able to change various people's files, the permissions and ownership will not be affected.

But please, please tar everything up first just in case it all goes horribly wrong. Paranoia is a good first step before going in and batch updating things :)

Jim

orbitz
04-23-2008, 06:10 PM
Thanks a lot Jim. Yah, back up is a must. :)

Czaries
04-23-2008, 10:38 PM
You also need to keep in mind that "<?=" is allowed where "<?php=" is not, so you won't be able to do a straight replace on tags that have an equal sign immediately following the short open tag. Instead, you will need to replace them with: "<?php echo ". The the replaced echo starements won't have a semicolon on the end, but they should still work without a parse error as long as they're on one line (the same as "<?=" have to be), so you should be fine with that one exception.

orbitz
04-23-2008, 11:36 PM
Thanks Czaries!