0
0
PHPprogramming~10 mins

Preg_match_all for global matching in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Preg_match_all for global matching
Start with input string
Apply preg_match_all with pattern
Scan entire string for all matches
Store all matches in array
Return number of matches found
Use matches as needed
End
The function scans the whole string, finds all parts matching the pattern, stores them, and returns how many matches it found.
Execution Sample
PHP
<?php
$input = "apple banana apple cherry";
preg_match_all('/apple/', $input, $matches);
print_r($matches[0]);
?>
This code finds all occurrences of 'apple' in the input string and prints them.
Execution Table
StepActionPatternInput StringMatches FoundOutput
1Start with input stringapple banana apple cherry
2Call preg_match_all/apple/apple banana apple cherry
3Scan string from start/apple/apple banana apple cherryFound 'apple' at pos 0
4Continue scanning/apple/apple banana apple cherryFound 'apple' at pos 13
5No more matches/apple/apple banana apple cherryTotal matches: 2
6Store matches in array/apple/apple banana apple cherry['apple', 'apple']
7Print matches/apple/apple banana apple cherry['apple', 'apple']Array ( [0] => apple [1] => apple )
8End execution
💡 No more matches found in the string, preg_match_all returns total count 2.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
$input"apple banana apple cherry""apple banana apple cherry""apple banana apple cherry""apple banana apple cherry""apple banana apple cherry"
$matchesempty['apple']['apple', 'apple']['apple', 'apple']['apple', 'apple']
Key Moments - 3 Insights
Why does preg_match_all find multiple matches instead of stopping at the first?
Because preg_match_all scans the entire string for all matches, not just the first one, as shown in steps 3 and 4 where it finds two 'apple' matches.
What is stored inside the $matches array?
The $matches array stores all matched strings found by preg_match_all, as seen in step 6 where both 'apple' matches are stored.
Why is the output printed from $matches[0]?
Because preg_match_all stores the full matches in $matches[0], which is why printing $matches[0] shows all matched strings, as in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many matches does preg_match_all find?
A2
B1
C3
D0
💡 Hint
Check the 'Matches Found' column in steps 5 and 6.
At which step does preg_match_all store all matches in the array?
AStep 3
BStep 4
CStep 6
DStep 7
💡 Hint
Look for the step mentioning storing matches in the 'Matches Found' column.
If the input string had no 'apple', what would preg_match_all return?
AAn array with one empty string
BAn empty array and zero matches
CAn error
DThe whole input string
💡 Hint
Think about what happens when no matches are found, as explained in the exit note.
Concept Snapshot
preg_match_all(pattern, string, matches) scans the whole string
Finds all parts matching pattern
Stores matches in matches array
Returns number of matches found
Use matches[0] for full matched strings
Full Transcript
This example shows how preg_match_all works in PHP. It takes a pattern and a string, then finds all parts of the string that match the pattern. It stores these matches in an array and returns how many matches it found. The code example finds all 'apple' words in the string 'apple banana apple cherry' and prints them. The execution table traces each step: starting with the input, scanning for matches, storing them, and printing the results. The variable tracker shows how the matches array fills up with each found match. Key moments clarify why preg_match_all finds multiple matches, what is stored, and why we print matches[0]. The quiz tests understanding of match count, storage step, and behavior when no matches exist. This helps beginners see how preg_match_all works step-by-step.