Replacing Text with preg_replace
Substitute matches with replacement strings and backreferences.
preg_replace() Signature
preg_replace($pattern, $replacement, $subject) returns the subject with all matches replaced. Pass an array of subjects to process multiple strings at once.
Simple Replacement
Replace all digits in a string with an asterisk.
<?php
$result = preg_replace('/\d/', '*', 'Order #12345');
echo $result; // "Order #*****"All lessons in this course
- Regex Syntax Fundamentals
- Matching with preg_match
- Replacing Text with preg_replace
- Splitting and Practical Patterns