Domanda

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)
È stato utile?

Soluzione 2

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])

Altri suggerimenti

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*,([^)]*))

If you need a WIDER spectrum when upgrading the PHP version rather than JUST this upper use case:

Didn't clean it up, but it should catch every instance I could think of.

Search:

array_key_exists\s*\(\s*([^,]*)\s*,\s*((\(\w+\))?[a-z0-9_$'"\{\}\[\]\-\>\:]*(\(\))*[a-z0-9$_\.\{\}\'\"\[\]\-\>\:]*)\)

Replace:

isset($2[$1])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top