Updating PHP code using Dreamweaver and Regular Expressions

Published on
9,699 Points
Last Modified:
Community Pick
Shaun McNicholas
Out of the box developer, creative thinker, 10,000 to 100,000 foot leader in all things development and marketing
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 websites. 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 gets 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 .= “

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 of that regular expression:
What this regular expression is saying is this: Find anything that begins with $ followed by anything in the alphanumeric range 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.  The \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.  The end result is that the string
    $variable .=
is replaced by
    if (!isset($variable)) { $variable=”"; }
meaning if the variable is not defined first, then define it and then continue processing.

Problem 2
The other method in this application that is no longer supported in PHP 5.3.1: Using the
    if (!$variable)
condition to determine if a variable exists.  Alas, that appeared in this application in 4,000+times!  under 5.3.1, you must now ask the code
    if (!isset($variable))
so my solution using the same method regulr expression tools

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  decided I would document it in this Experts-Exchange article where many programmers could find it.

I hope it helps you, too!

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Join & Write a Comment

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month