Preg_replace helps you find parts of text that match a pattern and replace them with new text. It is useful when you want to change or clean up text automatically.
Preg_replace for substitution in PHP
<?php mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) ?>
$pattern is the regular expression pattern to find.
$replacement is the text to replace the matched parts.
$subject is the text where the search and replace happens.
$limit controls how many replacements to make (default is all).
<?php $text = "Hello 123, call me at 555-1234."; $new_text = preg_replace('/\d+/', 'NUMBER', $text); echo $new_text; ?>
<?php $text = "abc def ghi"; $new_text = preg_replace('/def/', 'xyz', $text, 1); echo $new_text; ?>
<?php $text = "No numbers here!"; $new_text = preg_replace('/\d+/', 'NUM', $text); echo $new_text; ?>
This program finds phone numbers in the text and replaces them with the word '[PHONE]'. It prints the text before and after the change.
<?php // Original text with phone numbers $original_text = "Contact us at 123-456-7890 or 987-654-3210."; // Show original text echo "Before replacement:\n" . $original_text . "\n"; // Replace phone numbers with [PHONE] $pattern = '/\d{3}-\d{3}-\d{4}/'; $replacement = '[PHONE]'; $modified_text = preg_replace($pattern, $replacement, $original_text); // Show modified text echo "After replacement:\n" . $modified_text . "\n"; ?>
Time complexity depends on the length of the text and complexity of the pattern, usually O(n) where n is text length.
Space complexity is O(n) because a new string is created with replacements.
Common mistake: forgetting to escape special characters in the pattern (like \d for digits).
Use preg_replace when you need powerful pattern matching. For simple fixed string replacements, str_replace is faster.
preg_replace finds text matching a pattern and replaces it.
You can control how many replacements happen with the limit parameter.
It is useful for cleaning or formatting text automatically.
Practice
preg_replace do?Solution
Step 1: Understand the purpose of preg_replace
The function preg_replace searches a string for parts that match a pattern and replaces those parts with new text.Step 2: Compare with other functions
Counting matches or splitting strings are done by other functions like preg_match_all or preg_split, not preg_replace.Final Answer:
It finds text matching a pattern and replaces it with new text. -> Option CQuick Check:
preg_replace replaces matched text [OK]
- Confusing preg_replace with preg_match
- Thinking preg_replace counts matches
- Mixing preg_replace with string splitting functions
Solution
Step 1: Identify the pattern for digits
The pattern '\d' matches any single digit (0-9). In PHP single-quoted strings, it is written as '/\d/'.Step 2: Check other options
B uses '/d/', which matches the literal letter 'd'. C uses '/[0-9]+/', which matches one or more consecutive digits, replacing the entire group with a single '#'. D uses '/\D/', which matches non-digits.Final Answer:
preg_replace('/\d/', '#', $text); -> Option DQuick Check:
Use '\d' to match digits [OK]
- Forgetting to escape backslash in PHP strings
- Using 'd' instead of '\d' for digits
- Confusing '\d' with '\D' (non-digits)
$text = "Hello 123 World";
$result = preg_replace('/\d+/', 'NUM', $text);
echo $result;Solution
Step 1: Understand the regex pattern '/\d+/'
The pattern '\d+' matches one or more digits in a row. In the string, '123' matches this pattern.Step 2: Apply preg_replace to the string
The matched digits '123' are replaced by 'NUM'. So the string becomes 'Hello NUM World'.Final Answer:
Hello NUM World -> Option AQuick Check:
Digits replaced by 'NUM' = Hello NUM World [OK]
- Thinking only single digits replaced separately
- Expecting original digits to remain
- Confusing pattern with non-digit matching
$text = "abc123def";
$result = preg_replace('/[a-z]/', 1, $text, 2);
echo $result;Solution
Step 1: Check preg_replace argument order
The arguments are: pattern, replacement, subject, limit. The limit is correctly the fourth argument.Step 2: Check replacement type
The replacement should be a string. Using integer 1 will be converted to string '1' but is not recommended and can cause confusion.Step 3: Check other options
Pattern can be in single quotes. preg_replace supports limit parameter.Final Answer:
The replacement value must be a string, not an integer. -> Option BQuick Check:
Replacement should be string [OK]
- Passing integer instead of string as replacement
- Misplacing the limit parameter
- Thinking preg_replace can't limit replacements
'apple banana apple apple grape'. Which preg_replace call achieves this?Solution
Step 1: Understand the limit parameter in preg_replace
The fourth argument limits how many replacements happen. To replace only first two 'apple', limit should be 2.Step 2: Check each option's limit
preg_replace('/apple/', 'orange', 'apple banana apple apple grape', 2); uses limit 2, so only first two 'apple' replaced. preg_replace('/apple/', 'orange', 'apple banana apple apple grape'); replaces all. preg_replace('/apple/', 'orange', 'apple banana apple apple grape', 3); replaces first three. preg_replace('/apple/', 'orange', 'apple banana apple apple grape', 1); replaces only first one.Final Answer:
preg_replace('/apple/', 'orange', 'apple banana apple apple grape', 2); -> Option AQuick Check:
Limit 2 replaces first two matches [OK]
- Omitting limit to replace all occurrences
- Setting wrong limit number
- Confusing order of arguments
Ready to prove it?
Pass the test and this pattern moves unknown
