Solved

replace all variable in php using regex

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Six Sigma Control Plans

695 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