0

I'm using PHP to try to parse a string matching this kind of pattern :

field[foo][bar]

I use preg_match_all to get the indexes of the array ('foo' and 'bar' in this case). Here is my code, but it does not work :

preg_match_all("/\w+(\[(\w*)\])*/", $string, $matches);

And I'm getting these matches :

array

    (3) {
      [0]=>
      array(1) {
        [0]=> string(15) "field[foo][bar]"
      }
      [1]=>
      array(1) {
        [0]=> string(5) "[bar]"
      }
      [2]=>
      array(1) {
        [0]=> string(3) "bar"
      }
    }

and I would like something like that :


    array(3) {
      [0]=>
      array(1) {
        [0]=> string(15) "field[foo][bar]"
      }
      [1]=>
      array(1) {
        [0]=> string(5) "[foo]"
        [0]=> string(5) "[bar]"
      }
      [2]=>
      array(1) {
        [0]=> string(3) "foo"
        [0]=> string(3) "bar"
      }
    }

(Actually, I'm only interested in the last array, the one without the square brackets)

I don't know why I'm getting only the last match...

Any idea ? :)

Toto
  • 89,455
  • 62
  • 89
  • 125
Angelo
  • 198
  • 1
  • 14

1 Answers1

0

Have a try with:

preg_match_all("/(\[(\w*)\])/", $string, $matches);
Toto
  • 89,455
  • 62
  • 89
  • 125