Updating PHP code using Dreamweaver and Regular Expressions

AID: 3332
  • Status: Published

2010 points

  • Bymaestropsm
  • TypeTips/Tricks
  • Posted on2010-06-28 at 13:38:27
Awards
  • Community Pick
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 .= “

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 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!
Asked On
2010-06-28 at 13:38:27ID3332
Tags

PHP

,

Dreamweaver

,

Regular Expressions

Topic

Regular Expressions

Views
971

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Regular Expressions Experts

  1. kaufmed

    137,916

    Master

    70 points yesterday

    Profile
    Rank: Genius
  2. TerryAtOpus

    50,832

    Master

    0 points yesterday

    Profile
    Rank: Genius
  3. farzanj

    29,324

    0 points yesterday

    Profile
    Rank: Genius
  4. ozo

    15,408

    0 points yesterday

    Profile
    Rank: Savant
  5. Ray_Paseur

    13,176

    0 points yesterday

    Profile
    Rank: Savant
  6. wdosanjos

    11,800

    0 points yesterday

    Profile
    Rank: Genius
  7. ahoffmann

    10,800

    0 points yesterday

    Profile
    Rank: Genius
  8. bounsy

    8,600

    0 points yesterday

    Profile
    Rank: Wizard
  9. MichaelStaszewski

    8,372

    0 points yesterday

    Profile
  10. leakim971

    7,000

    0 points yesterday

    Profile
    Rank: Genius
  11. StingRaY

    6,000

    0 points yesterday

    Profile
    Rank: Wizard
  12. for_yan

    5,000

    0 points yesterday

    Profile
    Rank: Genius
  13. pfrancois

    4,448

    0 points yesterday

    Profile
    Rank: Guru
  14. dcsbeemer

    3,500

    0 points yesterday

    Profile
  15. CodeCruiser

    3,414

    0 points yesterday

    Profile
    Rank: Genius
  16. PaulHews

    3,400

    0 points yesterday

    Profile
    Rank: Genius
  17. woolmilkporc

    3,252

    0 points yesterday

    Profile
    Rank: Genius
  18. arnold

    3,000

    0 points yesterday

    Profile
    Rank: Genius
  19. Pryrates

    2,800

    0 points yesterday

    Profile
    Rank: Wizard
  20. Vaulden

    2,800

    0 points yesterday

    Profile
  21. nepaluz

    2,664

    0 points yesterday

    Profile
    Rank: Sage
  22. xterm

    2,600

    0 points yesterday

    Profile
    Rank: Sage
  23. HonorGod

    2,600

    0 points yesterday

    Profile
    Rank: Genius
  24. sentner

    2,600

    0 points yesterday

    Profile
    Rank: Wizard
  25. matthewspatrick

    2,400

    0 points yesterday

    Profile
    Rank: Savant

Hall Of Fame