Solved

replace all variable in php using regex

Posted on 2011-02-11
4
353 Views
Last Modified: 2012-05-11
I have a lot of php files and in order to standardize the coding structure, I need to make all array variable to be 'more' valid by adding quote in between.

For example:
$_POST[variablename] replaced to $_POST['variablename']
$_SESSION[variablename1][variablename2] replaced to $_SESSION['variablename1']['variablename2']
$_GET['variablename'] is to be skipped and do nothing because it's in good format

Now, I know I can use perl or awk to go over all files and using regex to insert the quote. What's the quickest way to achieve this and how do I do it?
0
Comment
Question by:edyonline
4 Comments
 
LVL 3

Expert Comment

by:flubbard
ID: 34878316
Possibly consider the sed command using the input as your file and outputting to a file.  Wrap this in a shell script to process the whole directory.

 - flub
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 34878361
Hmm... Before you go too far down this path, learn about version control using CSV or SubVersion or GIT.  Also, buy and read Martin Fowler's book about "Refactoring."

I have dealt with similar bad-coding-corrections in the past.  What I did was load the source code into a modification library and use a code scanner (available in TextPad or Notepad++) to identify the instances that were of interest.  You might scan for $_POST[ first to gauge the scope of the problem.  In Textpad, you can say, "find in files" and you can use a regular expression to make the search.  Don't forget that the brackets are meta-characters and may need to be escaped.

Textpad will give you a window with each found line.  A click on the line opens the file and leaves your cursor on the line in question.  If it is a small number of corrections (less than a thousand) it will probably be easier to make the corrections by hand.  You could spend a long time debugging the automation for something like this!

Best of luck, ~Ray
0
 
LVL 8

Assisted Solution

by:kivan24
kivan24 earned 167 total points
ID: 34878999
perl -pi -w -e 's/(\[)(\w+?)(\])/$1"$2"$3/g;' *.php

Open in new window

0
 
LVL 5

Accepted Solution

by:
group0 earned 167 total points
ID: 34902653
This type of change should not be automated with search and replace regular expressions unless you really really know what you're doing.  For instance, the one provided by kivan24 will induce a syntax error if it encounters a bareword key inside an interpolated string (which is perfectly valid):

$a = "foo $bar[baz]";
becomes
$a = "foo $bar["baz"]";

Even changing the replacement pattern to use single quotes will still cause breakage in this example.

Using regex to locate the possible offenders is fine ("egrep -r" is probably the easiest way to search an entire project), but too many things can subtly break when scripting this sort of change.  As noted by Ray_Paseur, if the source isn't already checked into a source code repo, it's a good idea to do so before beginning this project.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question