Solved

replace all variable in php using regex

Posted on 2011-02-11
4
349 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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MYSQL simple update statement 3 32
Modify PHP Code on the Fly? 8 39
session dropped in IE 10 19
Instering to MySQL table 5 16
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now