i need a regex to search this: $var[prod_menu]
and replace with this: $var['prod_menu']
At the moment my regex make this: $var['rod_men']
My regex:
preg_replace('/\[[^\'](.*)[^\']\]/U', '['$1']', $input_lines);
Where is the error?
You should start the capturing group after the opening [ and close before the ]
Note that this part in your pattern [^\'](.*)[^\'] uses 2 times a negated character class which actually expects a minimum of 2 characters.
Another way to write your pattern could be using a negated character class matching not [, ], $ or a '
\[([^]\[$']+)\]
In the replacement use
['$1']
Or make the match more restrictive matching 1+ word characters.
\[(\w+)\]
\[(\w+)\] See regex101.com/r/TL4QyL/1\[([^]\[$']+)\] See regex101.com/r/n7Ttjt/1Just change your capture group to include the extra chars at the start and end...
preg_replace('/\[([^\'].*[^\'])\]/U', "['$1']", $input_lines);
(I've also fixed the problem with quotes in '['$1']').
array[i])
_beforevar. What code have you really got?