0
0
PHPprogramming~10 mins

Preg_match for pattern matching in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Preg_match for pattern matching
Start with input string
Define regex pattern
Call preg_match(pattern, string)
Check if pattern matches string?
NoReturn 0 (no match)
Yes
Return 1 (match found)
Optionally fill matches array
End
The program takes a string and a regex pattern, uses preg_match to check if the pattern matches the string, returns 1 if yes, 0 if no, and optionally stores matched parts.
Execution Sample
PHP
<?php
$pattern = '/cat/';
$string = 'The cat sat.';
if (preg_match($pattern, $string, $matches)) {
    echo "Match found: " . $matches[0];
} else {
    echo "No match.";
}
?>
This code checks if 'cat' is in the string and prints the matched word if found.
Execution Table
StepActionPatternStringpreg_match ResultMatches ArrayOutput
1Define pattern/cat/N/AN/AN/AN/A
2Define stringN/AThe cat sat.N/AN/AN/A
3Call preg_match/cat/The cat sat.1["cat"]N/A
4Check resultN/AN/A1 (true)N/AN/A
5Print matchN/AN/AN/AN/AMatch found: cat
💡 preg_match returns 1 because 'cat' is found in the string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$patternundefined/cat//cat//cat//cat/
$stringundefinedundefinedThe cat sat.The cat sat.The cat sat.
$matchesundefinedundefinedundefined["cat"]["cat"]
Key Moments - 3 Insights
Why does preg_match return 1 instead of true or false?
preg_match returns 1 for a match, 0 for no match, and false on error. This is shown in execution_table row 3 where result is 1 meaning a match was found.
What is stored in the $matches array?
The $matches array stores the matched substring(s). In row 3, it contains ["cat"], the exact part of the string that matched the pattern.
What happens if the pattern is not found in the string?
preg_match returns 0 and $matches remains empty or unchanged. This is implied by the exit_note and would be shown if the string did not contain 'cat'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $matches after step 3?
A[]
B["cat"]
Cnull
D["dog"]
💡 Hint
Check the 'Matches Array' column in row 3 of the execution_table.
At which step does preg_match confirm a match was found?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'preg_match Result' and 'Action' columns in the execution_table.
If the string was 'The dog sat.', what would preg_match return?
A0
Bfalse
C1
Dnull
💡 Hint
Refer to the exit_note and understand preg_match returns 0 when no match is found.
Concept Snapshot
preg_match(pattern, string, matches) checks if pattern matches string.
Returns 1 if match found, 0 if not, false on error.
Matches array stores matched parts.
Use /pattern/ syntax for regex.
Commonly used for simple pattern checks.
Full Transcript
This example shows how preg_match works in PHP. First, we define a regex pattern '/cat/'. Then we have a string 'The cat sat.'. Calling preg_match with these checks if 'cat' is inside the string. It returns 1 because it finds a match. The matched word 'cat' is stored in the matches array. If no match was found, preg_match would return 0. This helps us decide what to do next, like printing 'Match found: cat' or 'No match.'.