Archive for February, 2010

Updating PHP code using regular expressions

I have been reconstructing a php based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building php sites. I am not incredibly fond of writing php code on a daily basis and have been working on getting the system migrated to an up to date implementation of php 5.3.1 and I’ve run across some issues in the migration that I thought warranted documenting.

Problem 1) The application as it stands is currently on a Linux box running php 4.4.2 which allows you to use variables without pre-defining them. So if you want to write a conditional loop that takes a variable named $var and loop through a query adding things to the variable. You don’t need to pre-define the variable you just put in the loop $var .= “new conditions” and the variable get’s appended including the new string. The problem is security of course and the most recent implementations do not allow a variable to be appended unless it pre-exists. So I needed to devise a way to find every existence of the $var .= no matter what the variable was called and then append the code so that it now says ” if (!isset($variable)) { $variable=”";} $variable .= “.

Solution: Adobe Dreamweaver (or any other API that includes a find and replace utilizing regular expressions) I just like using Dreamweaver because I’ve been using it for so long. I am sure you can do the same thing in many other APIs like Eclipse of BBEdit or whatever your preferred editor. If you aren’t using something capable of doing site wide find and replace with regular expressions GET ONE! This method has saved me weeks of coding by allowing me to do global or site wide replacement of outdated code.

Here was my final solution:
In the FIND BOX: ([\\$]\w*\b) \.\=
In the REPLACE BOX: if (!isset($1)) { $1 = “”; } $1 .=

In my application instance there were a total of 10,876 instances of a variable followed by the .= operator. Imagine hand coding that many instances of variables that are now out of date!

Explanation:
What this regular expression is saying is this. Find anything that begins with $ followed by anything in the alphanumeric keybord 1 through 0 as well as a through z and A through Z. That’s denoted by the \$\w the [] signs surround the \\$ to denote that I am looking for the dollar sign and not wanting to use the dollar sign as a special character predefined by the regular expression syntax. The \w denotes a word or series of characters and the *\b denotes continue to the next end of line \b meaning a space or line return. The expression inside a set of parentheses means it’s going to capture the expression in the return statement as the first in a registry of expressions denoted in the replace statement as $1. So my replace statement includes whatever I want to replace the entire string with including $1 as the original expression. So the string $variable .= is replaced by if (!isset($variable)) { $variable=”"; } meaning if the variable is not defined first define it and then continue with the logic.

Problem 2) The other method in this application that is no longer supported in PHP 5.3.1: Using the if (!$variable) to determine if a variable exists which again exists in this application in 4,000+ instances! You now have to ask the code if (!isset($variable)) so my solution using the same method:

In the FIND BOX: if \(\!([\\$]\w*\b)
In the REPLACE BOX: if (!isset($1)

I just thought this regular expression syntax was amazing! So I though I would document it for the world to be able to find.
Hope it helps if you are a programmer!

No Comments