Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

replace all variable in php using regex

Posted on 2011-02-11
4
Medium Priority
?
362 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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 668 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 668 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …
Suggested Courses

636 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