Jump to content

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.

 

 

 

 

1 hour ago, marmelade7 said:

But how DO I count the matches? I've already looked at examples but I still don't get it.

Did you check the link I gave? It pointed you to the Return Values section (or at least it was supposed to).

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.

 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.

22 minutes ago, marmelade7 said:

I need someone to explain where the answer is.

That's already been done (twice). I suggest you learn to read before embarking on a programming career.

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.

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.