TheKidsWantDjent
TheKidsWantDjent

Reputation: 1269

preg_match_all doesn't return anything

I hope you can help me with this. I'm completely puzzled about this problem. Somehow my preg_match_all doesn't return anything. It's supposed to return either an error or an integer, but it doesn't. Error_reporting is on and everything, I just can't think of anything wring with this.

echo $string = '234,2345,34534,223'.'<br>';
echo preg_match_all('/,[0-9][0-9]/', $string).'<br>';

You can see the $string but the second echo doesn't do anything. I guess you'll need more information on this, but I just don't have any idea where this problem lays.

Edit: running PHP 4.3

Upvotes: 1

Views: 131

Answers (2)

anubhava
anubhava

Reputation: 785196

You need to provide 3rd parameter as capture array in preg_match_all and check its return value first. Use it like this:

if (preg_match_all('/,\d{2}/', $test, $match)) {
   print_r($match);
}

Upvotes: 0

Andrew Cheong
Andrew Cheong

Reputation: 30273

preg_match_all returns the number matches found, or FALSE. You want to pass in a third parameter, a referenced array, and print_r that if you want to see the results.

echo $string = '234,2345,34534,223'.'<br>';
preg_match_all('/,[0-9][0-9]/', $string, $matches);
print_r($matches);

(Also, you're using $string then $test but I assume that's just a typo in the question.)

Upvotes: 1

Related Questions