Solved

replace all variable in php using regex

Posted on 2011-02-11
4
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 i…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
The viewer will learn how to count occurrences of each item in an array.
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…

734 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