Jump to content

How do I count results in a nested array generated by Preg_Match


marmelade7

Recommended Posts

My goal is to reliably count the number of matches in a preg_match_all operation.
 

The following code works fine in a separate PHP page.  However, it does NOT work when I include it in other php code.  Same code: different result.

 

preg_match_all('/[0-9]{1,}[-]{1}[0-9]{1,}/',$txt,$matches);
$c=count($matches[0]);
preg_match_all('/[A-Za-z]{1,}[-]{1}[A-Za-z]{1,}/',$txt,$matches);
$d=count($matches[0]);

echo "c count=".$c;

echo "d count="$d; 

Why does this code NOT work when I include it in other PHP code, even though this code works in a php page by itself?

 

How can I reliably count the number of matches in a preg_match_all operation.  I know I use "matches" and I know there are nested arrays.  However the statement count($matches[0]) does not always work.

 

 

 

 

Link to comment
Share on other sites

I've already been through all that stuff.  I need someone to explain where the answer is.  I've deduced that count($matches[0]) contains the answer, but count($matches[0]) does not always work. The examples show me how to print the results in the array -- I still do not see how to count them.

Link to comment
Share on other sites

 I was hoping for a specific example that shows a preg_match_all statement that ends up with something like: $totalMatchedItems=5.  The examples I see show a bunch of array info, but don't make it clear (at least not to me) how to count these nested arrays.

Link to comment
Share on other sites

4 hours ago, marmelade7 said:

I've already been through all that stuff.  I need someone to explain where the answer is.  I've deduced that count($matches[0]) contains the answer, but count($matches[0]) does not always work.

It seems like you are fixed on using $matches.  People tried a few different ways to tell you that the function returns the number of matches, so this is your original code re-written.

$c = preg_match_all('/[0-9]{1,}[-]{1}[0-9]{1,}/', $txt, $matches);
$d = preg_match_all('/[A-Za-z]{1,}[-]{1}[A-Za-z]{1,}/', $txt, $matches);

echo "c count=$c";
echo "d count=$d";

 

The reason for this, is that any regex can have groupings (capture groups) that produce partial matches, and all of these are returned in the $matches array.  The return value only returns the actual number of full matches, which is clearly what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.