Valor_ Valor_ - 1 month ago 6
PHP Question

Regex for creating proper php array structure from text

I'm having a problem with proper regex expression for my case. So i have this "text" which i need to convert in to the PHP array with find an replace function in my code editor

$grid['added'][0][qwer'] = 'asda';
$grid['added'][0][tzui'] = 'asda';
$grid['added'][1][sdfg'] = 'asda';
$grid['added'][2][ghjk'] = 'asda';
....
$grid['added'][4][nbmh'] = 'asda';
$grid['added'][666][fghz'] = 'asda';


desired output

$grid['added'][0]['qwer'] = 'asda';
$grid['added'][0]['tzui'] = 'asda';
$grid['added'][1]['sdfg'] = 'asda';
$grid['added'][2]['ghjk'] = 'asda';
....
$grid['added'][4]['nbmh'] = 'asda';
$grid['added'][666]['fghz'] = 'asda';


So far i'm trying to fetch all numbers inside square brackets, then add that apostrophe but editor doesn't find any thing for the following regex.

/\[[0-9]+\]/


So how should my regex look like so i can add that apostrophe to my "text" in order to have proper array structure for my code. If you need any additional information's, please let me know and i will provide. Thank you

Answer

Find:

\['?([a-zA-Z]+)'?\]

Replace:

['$1']

This finds any set of letters within square brackets, that may or may not have any quotes around it, and makes sure there are two quotes.