0

Whats the best way to do a regex search and replace for all instances of array_key_exists() with the more efficient isset()? Please, no Donald Knuth quotes regarding optimizations and yes, I'm aware of the differences between the two functions.

This is what I'm currently using in my Netbeans search and replace:

  • search for:

    array_key_exists\s*\(\s*'([^']*)'\s*,([^)]*)\) 
    
  • replace with:

    isset($2['$1'])
    

it works well , changing this:

array_key_exists('my_key',$my_array)

to

isset($my_array['my_key'])

but doesn't pick up instances like this:

array_key_exists($my_key,$my_array)
Community
  • 1
  • 1
AndrewD
  • 4,397
  • 2
  • 29
  • 31

2 Answers2

0

Not the most elegant solution, but adding to your current regex we find both types of search criteria.

array_key_exists\s*(\s*'|$['|\S]\s*,([^)]*))

B.Bart
  • 53
  • 1
  • 5
  • this doesn't work for me.I tried it in the netbeans engine and a few others regex testers in line. – AndrewD Mar 10 '14 at 04:19
0

The best I could do was to run a second search and replace to cover the instances that used variables for both arguments:

array_key_exists($my_key,$my_array)

search and replace 2:

  • search for:

    array_key_exists\s*\(\s*(\$[^,]*)\s*,([^)]*)\)

  • replace with:

    isset($2[$1])

AndrewD
  • 4,397
  • 2
  • 29
  • 31