Skip to content

Instantly share code, notes, and snippets.

@nelsonr
Created July 5, 2012 09:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nelsonr/3052693 to your computer and use it in GitHub Desktop.
Save nelsonr/3052693 to your computer and use it in GitHub Desktop.
Regex for PHP associative array keys that lack quotes
Regex for PHP associative array keys that lack quotes
------------------------------------------------------
Find with: \$([a-z0-9_]*)\[(?!('|"))([a-z0-9_]*)(?!('|"))\]
Replace with: $\1['\3']
\1 and \3 relate to groups in the expression that are whatever what's inside parentheses.
The first and third group ([a-z0-9_]*) are equal and each one
matches the variable name and the key name respectively
------------------------------------------------------
Matches the following:
$your_var_name[your_assoc_key]
Which will turn into:
$your_var_name['your_assoc_key']
------------------------------------------------------
Won't match:
$your_var_name["your_assoc_key"] or $your_var_name['your_assoc_key']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment