0

I have some severely deprecated PHP code that I'm sifting through. One of the problems that I have is hundreds of errors like this:

[Mon Dec 09 07:00:33 2013] [error] [client 127.0.0.1] PHP Notice: Use of undefined constant id - assumed 'id' in /home/srv/site.local/content/online.php on line 171, referer: http://site.local/index.php

These result in the practice of a previous coder of calling arrays like this:

$array[some_element]

instead of:

$array['some_element']

So I know how to fix that by going through each file and adding the quotes, over, and over, and over again. My question is how to write script for this. I imagine this might be a sed or awk script, but I'm not sure. I'm going to start working on a sed as soon as I'm done posting this, but any suggestions?

3
  • why do not you just replace $array[some_element] with this $array['some_element'] using any editor? Commented Dec 9, 2013 at 12:28
  • Because I'd be here all day. This is throughout the site; and it's a big site.. I use vim. It probably is script-able in vim. I do have bluefish, though. I would like to get my head around sed and awk, but the point is to get it done. Good idea. I take a look at bluefish and see if it lets me do a global replace. Commented Dec 9, 2013 at 12:40
  • Here's where I'm at with awk right now, which looks more suited than sed: awk '{c += sub(/\[[.*]\]/,"[element]") }1' content/testfile.php | less which is not quite doing anything yet, but the point is to at least "hit" the array elements first. Commented Dec 9, 2013 at 12:42

2 Answers 2

1

I don't know anything about php, so I'm not sure whether this solution is a particularly good one:

sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in

example of use:

[fixarraysubscript $] cat test.in
$array[some_element]

$name1[index]

$name2[index]


$name3[ id ]

$name4[ id2 ]

[fixarraysubscript $]
[fixarraysubscript $] sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in
$array['some_element']

$name1['index']

$name2['index']


$name3['id']

$name4['id2']

[fixarraysubscript $]

obviously my input file is somewhat contrived. If this doesn't work for you, please feel free to post some real input.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! That hits the simpler cases and does what I need to do with them. That will probably clear up 95% of my errors.
ok, great. I now see your more complex example, and this won't correctly transpose the first outer brackets of something like: $tsms_cats[$data['t_s']]['color']. I guess it depends on how many examples you have like this...
Right. I just ran it and it turns that bit into: $tsms_cats['$data['t_s']'][''name''] So I'm working on the regex a bit. I'm pretty sure I can limit it to word characters and avoid that.
I thought I would have to use back references for this. Interesting how you avoided that.
Well, maybe I do... This is getting really close. Thank you for your help: sed "s|\[\(\w\)|\[\'\1|g;s|\(\w\)\]|\1\'\]|g" content/testfile.php | grep tsms_cats ...$tsms_cats[$data['t_s']]['color']...
|
0

This may work with gnu awk

echo '$array[some_element]' | awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1["q"\\2"q"]","g")}' q="'"
$array['some_element']

For your file

awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1["q"\\2"q"]","g")}' q="'" file

or

awk '{print gensub(/\$([^[]+)\[([^]]+)]/,"$\\1[\x27\\2\x27]","g")}' file

EDIT: Changed regex to reflect variable array name.

4 Comments

"function gensub undefined", but I think the regex is getting close. Thank you. I might not have been clear, BTW, but this syntax was used on every array in every file on a large website. Plus, to complicate things further, a few arrays do have quotes in them. I haven't seen any multidimensional arrays in there yet, but I bet there are some...
Of course if I can get something that works one file I can just throw it in a bash for loop to get all...
Here's the most complex example I see so far: $tsms_cats[$data[t_s]]['color'] So I need to figure out how to replace that with: $tsms_cats[$data['t_s']]['color'] As well as the simpler ones. I suspect that this will be multiple awks in a bash script by the time I get done. Still better than going through and changing all of this "by hand" though, IMO.
If you do not have gnu awk why you seen undefined using gensub. Your code would be hard to match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.