0PricingLogin
PHP Academy · Lesson

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

  1. Regex Syntax Fundamentals
  2. Matching with preg_match
  3. Replacing Text with preg_replace
  4. Splitting and Practical Patterns
← Back to PHP Academy