Recursive search based on dynamic pattern?

n00b0101
n00b0101 used Ask the Experts™
on
So, I inherited some really bad code and am currently just trying to refactor it to the point of functionality before completely overhauling it.  One of the problems is that the original programmer set posts and sessions like this:

$_SESSION[somename]  
$_POST[somevar]

Open in new window

instead of:
$_SESSION['somename']
$_POST['somevar']

Open in new window


It seems to be causing problems throughout the site.  Is there a fast way that I can recursively find and replace these types of variables?  They're everywhere and obviously are named different, so I would need to find any variable that does not have the quotes and replace it with the same string that does?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Yeah, I feel for you.  I've had to go through the very same thing.

Unfortunately, the only way I know of to be able to fix the problem is to search and replace one by one.  If you are using Dreamweaver or some other program that is capable of searching all the files of your site, you can do a search for "_SESSION[" and set the replacement as "_SESSION['".  You would find the variable occurrence and replace that occurrence then click near the right bracket to insert the other quote.  You would need to do a similar process for the POST variable.

There is another possible way which would be to take the files one by one and open them in a programmable text editor like Word.  You would need to program up a macro to find, replace then search for the next occurrence of the end bracket and add the last quote.  The only problem here is that you could make your problems even greater than before because Word will add things to your file that you may not want.
Topic Advisor
Most Valuable Expert 2016
Commented:
Hi,

This is possible with sed, assuming you have a pattern for your variables ( [a-z]* in sample ):

sed "s/\$_POST\[\([a-z]*\)\]/\$_POST\[\\'\1\\']/g;s/\$_SESSION\[\([a-z]*\)\]/\$_SESSION\[\\'\1\\']/g" <yourfile>

If you want to replace in <yourfile> use:

sed -i "s/\$_POST\[\([a-z]*\)\]/\$_POST\[\\'\1\\']/g;s/\$_SESSION\[\([a-z]*\)\]/\$_SESSION\[\\'\1\\']/g" <yourfile>

Note: perform the last line only once per file or you will get double '' characters.

Sample:

cat test.txt

$_SESSION[somename]  $_POST[someothervar]
$_POST[somevar]

sed -i "s/\$_POST\[\([a-z]*\)\]/\$_POST\[\\'\1\\']/g;s/\$_SESSION\[\([a-z]*\)\]/\$_SESSION\[\\'\1\\']/g" test.txt

cat test.txt

$_SESSION['somename'] $_POST['someothervar']
$_POST['somevar']

You may need to change your variable name pattern, if you leave it out substitution will not work in case you have multiple occurrences on one line.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Interesting.  What problems is this notation causing?  I am asking because PHP is very forgiving of sloppy code.  Here is an example:

$_POST[somevar]

With this, PHP will first look for a defined constant named somevar and if the constant is found, it will be used as the array index into $_POST.

If the constant is not found, PHP will emit a Notice-level message, which may or may not be sent to the browser output stream, depending on the settings for error_reporting().  You can suppress Notice messages with error_reporting(E_ALL ^ E_NOTICE);

Next PHP will pretend that the quotes were there like this: 'somevar' and will try to resolve the variable reference with a named index.  So the net takeaway about this sloppy code is that if you turn off the Notice messages, PHP may do what the original programmer intended.

All that said, I would still remediate the code, and I would probably do it by hand because if you have some sloppy code without quotes, you may also have some sloppy code with quotes.  I would use an editor like Textpad and use the find-in-files capability to look for the opening bracket [.  This would give me a list of the scripts that needed attention and the line numbers.  Then armed with that inventory, I'd plow into the code and commit each script as I finish the changes to the array index notation.
Most Valuable Expert 2011
Top Expert 2016

Commented:
@n00b0101: Suggest you back up your files before you try to use the accepted solution.  You may want to restore and start over.  Also suggest you look for other arrays with similar notation.  If there is sloppy code around SESSION and POST, you may find similar constructs in other parts of the scripts.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial