marmelade7 Posted July 20, 2023 Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/ Share on other sites More sharing options...
requinix Posted July 20, 2023 Share Posted July 20, 2023 If it works in one file and not in another then you likely have some sort of problem with variables. Namely, that $txt one. Side note: don't count the matches like that. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610614 Share on other sites More sharing options...
marmelade7 Posted July 20, 2023 Author Share Posted July 20, 2023 But how DO I count the matches? I've already looked at examples but I still don't get it. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610615 Share on other sites More sharing options...
requinix Posted July 20, 2023 Share Posted July 20, 2023 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). Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610616 Share on other sites More sharing options...
marmelade7 Posted July 20, 2023 Author Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610625 Share on other sites More sharing options...
Barand Posted July 20, 2023 Share Posted July 20, 2023 From the link requinix gave you to the preg_match_all() function in the manual ... Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610626 Share on other sites More sharing options...
marmelade7 Posted July 20, 2023 Author Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610627 Share on other sites More sharing options...
marmelade7 Posted July 20, 2023 Author Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610628 Share on other sites More sharing options...
Barand Posted July 20, 2023 Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610629 Share on other sites More sharing options...
requinix Posted July 20, 2023 Share Posted July 20, 2023 preg_match_all returns the number of matches. Come back next week when your head has cooled off. Or don't. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610636 Share on other sites More sharing options...
gizmola Posted July 20, 2023 Share Posted July 20, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317100-how-do-i-count-results-in-a-nested-array-generated-by-preg_match/#findComment-1610645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.