-1

i need to parse parameters from a string using regex.

my example string:

'my page',1234,'main', '800,600', 45

desired output:

Array(
[0] => my page
[1] => 1234
[2] => main
[3] => 800,600
[4] => 45
)
Fuxi
  • 7,611
  • 25
  • 93
  • 139

1 Answers1

3

It looks like what you have is comma-separated values. Perhaps str_getcsv() will serve you better, since it can already account for commas which are inside of quoted strings.

print_r(str_getcsv("'my page',1234,'main', '800,600', 45", ',', "'"));
Array
(
    [0] => my page
    [1] => 1234
    [2] => main
    [3] => 800,600
    [4] =>  45
)

This doesn't trim the space before the "45" in item 4, but if that's necessary, we can use array_map() and trim().

print_r(array_map('trim', str_getcsv("'my page',1234,'main', '800,600', 45", ',', "'")));
Array
(
    [0] => my page
    [1] => 1234
    [2] => main
    [3] => 800,600
    [4] => 45
)
Garrett Albright
  • 2,844
  • 3
  • 26
  • 45