why preg_match_all does not return the number of matches

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
welkin
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 9:38 am

why preg_match_all does not return the number of matches

Post by welkin »

My regex looks like

X[^\x{4e00}-\x{9fa5}]*Y

(where X and Y are two Chinese characters)

and my expression looks like

preg_match_all("/".$regex."/iu", $content, $out, PREG_PATTERN_ORDER)

it turns out that preg_match_all is always returning 1, even though count($out[0]) is > 1 (and correctly so). So I ended up using count($out[0]) rather than the value this function returns. Though this solves the current issue I need, I am very curious why this is happening. Am I misunderstanding preg_match_all?

I'd greatly appreciate if anyone can point me to the explanation of this problem. Thanks in advance!
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: why preg_match_all does not return the number of matches

Post by akuji36 »

welkin
Forum Newbie
Posts: 2
Joined: Mon Oct 26, 2009 9:38 am

Re: why preg_match_all does not return the number of matches

Post by welkin »

Thanks but the about.com page makes no mention of the return value.

On php.net
http://php.net/manual/en/function.preg-match-all.php

It says "Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred."

Hmm... so I'm still lost. Anyone?
Post Reply