0

The below code is from a php book, when i run i am getting the message, Notice: Uninitialized string offset: 2

 // This function will put all of the matches found into
 // the $matches array
    preg_match("/\d/", "1 and 2 and 3 and 4", $matches);
 // The 0 element of the $matches array holds another
 // array of the matches.
    echo "Value: " . $matches[0][2] . "\n";

should i have to use print_r instead of echo?

Code
  • 219
  • 2
  • 11

1 Answers1

2

preg_match doesn't set $matches to a multi-dimensional array. It's an array where [0] is the matched element, and the remaining elements are the matches for all the capture groups.

You should be using preg_match_all.

Barmar
  • 741,623
  • 53
  • 500
  • 612