We help IT Professionals succeed at work.

RegEx or Text Replacement for PHP Associative

254 Views
Last Modified: 2012-05-05
Hello,
I have a large amount of PHP code that I have taken over from another programmer. However, the previous developer's programming style has been throwing me tons of notice of use of undefined constant's because he didn't quote the array values like the PHP manual recommends. I need a way to easily replace all these values with proper quoting. This is easy enough to do with a simple regex but the problem I am having is that it cannot quote array values that are already in a quoted string.

$array[value] needs to become $array['value']

and
"$array[value]" should be left alone.
Comment
Watch Question

It is not very simple, because regex is suppose to catch "some text \" text $array[value] $array[value] text \" " what is not trivial.
Also you may have the $array['text']['text'] statements.
The simple $array[value] to $array['value'] replacing can be done with the following regexp:
(\$[a-z_0-9]+\[|'\]\[)([A-Za-z0-9]+)(\]) -> $1'$2'$3 applied multiple times to handle $a[b][c] situations.

Author

Commented:
What about the regex not operator [^] to weed out values in quotes? I don't have that much experience with the not operator myself, however if I run that regex it will break all the code as there are LOTS of quoted variables.
It is not so easy.
For example:
$result = "asdf $a[test]" . '"\' . "$a[test] . " . '\"';

Is the $a[test] in quotes or not? There can be a lot of such tricky situations and you never know whether the regexp breaks something or not.


This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
dooh, please correct my typos:
  s/wont/won't/
  perl line misses final ;
  s/keen/keep/
  s/fals /false /
sorry for typing too fast ;-)
CERTIFIED EXPERT

Commented:
MIRV, if you wanted to do this in PHP (although I would recommend against doing the mass replacement):
<?php
$input='$array[]=Array();
$array[\'value\']="test";
echo "$array[value]";
echo $array[value];
echo "test".$array[value];
echo \'\" test \'.$array[value]."test";
echo "test\" test $array[value] \" test"
echo "test\"".$array[value]."test \" test"';
function quotevalues($matches) {
  if ($matches[1]) {
    return "$matches[1]'$matches[2]'$matches[3]";
  } else {
    return $matches[0];
  }
}
$output=preg_replace_callback('/(?<!\\\\)".*?(?<!\\\\)"|(?:(\$[\w-]+\[)([^\']+?)(\]))/','quotevalues',$input);
echo "<pre>$output";
?>

Open in new window

Author

Commented:
regex won't work from vim but I suppose I can solve that problem on my end..
> regex won't work from vim ..
regex work in vim too, but they are not perl regex, hence you need to adapt them for vim

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.