Valor_
Valor_

Reputation: 3591

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

Upvotes: 1

Views: 58

Answers (1)

user1919238
user1919238

Reputation:

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.

Upvotes: 2

Related Questions