0
0
PHPprogramming~10 mins

Preg_replace for substitution in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Preg_replace for substitution
Input string
Define pattern and replacement
Call preg_replace
Search string for pattern matches
Replace matches with replacement
Return new string with substitutions
The flow shows how preg_replace takes an input string, finds parts matching a pattern, replaces them, and returns the new string.
Execution Sample
PHP
<?php
$input = "Hello 123 World 456";
$output = preg_replace('/\d+/', 'NUM', $input);
echo $output;
?>
This code replaces all groups of digits in the input string with 'NUM'.
Execution Table
StepActionPattern MatchReplacementResulting String
1Start with input stringN/AN/AHello 123 World 456
2Find first match '\d+'Matches '123'Replace with 'NUM'Hello NUM World 456
3Find second match '\d+'Matches '456'Replace with 'NUM'Hello NUM World NUM
4No more matchesN/AN/AHello NUM World NUM
💡 No more digit groups found, substitution complete.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$input"Hello 123 World 456""Hello 123 World 456""Hello 123 World 456""Hello 123 World 456"
$outputundefined"Hello NUM World 456""Hello NUM World NUM""Hello NUM World NUM"
Key Moments - 3 Insights
Why does preg_replace replace all matches, not just the first?
preg_replace by default replaces all matches in the string, as shown in execution_table rows 2 and 3 where both '123' and '456' are replaced.
What does the pattern '/\d+/' mean?
It means 'one or more digits'. This is why '123' and '456' are matched and replaced, as seen in execution_table rows 2 and 3.
Why is the original $input variable unchanged?
preg_replace returns a new string with replacements; it does not modify the original string, as shown in variable_tracker where $input stays the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the string after the first replacement?
A"Hello NUM World NUM"
B"Hello NUM World 456"
C"Hello 123 World NUM"
D"Hello 123 World 456"
💡 Hint
Check execution_table row 2 under 'Resulting String'.
At which step does preg_replace stop finding matches?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at execution_table row 4 where no more matches are found.
If the pattern was changed to '/World/', what would be the output after replacement?
A"Hello 123 NUM 456"
B"654 MUN 321 olleH"
CHello 123 NUM 456"
D"Hello 123 NUM 456
💡 Hint
Replacing 'World' with 'NUM' changes only that word; digits remain unchanged.
Concept Snapshot
preg_replace(pattern, replacement, string) searches the string for parts matching the pattern.
It replaces all matches with the replacement text.
Returns a new string; original string stays unchanged.
Patterns use regular expressions, like '/\d+/' for digits.
Useful for flexible text substitutions.
Full Transcript
This visual trace shows how PHP's preg_replace function works step-by-step. Starting with the input string 'Hello 123 World 456', it looks for parts matching the pattern '/\d+/', which means one or more digits. It finds '123' first and replaces it with 'NUM', then finds '456' and replaces it too. The original string remains unchanged, and the function returns a new string with all digit groups replaced by 'NUM'. The process stops when no more matches are found. This helps beginners see how preg_replace scans and replaces all matches, not just the first, and how the pattern controls what is matched.