PHP Modules Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
preg_replace() - Replace Matched Substring
This section provides a tutorial example on how to search and replace regular expression patterns using preg_replace() and preg_match_filter() functions.
If you want to search and replace substrings, you can you can use the preg_replace() or preg_filter() function:
$outputs = preg_replace($patterns, $replacements, $subjects);
$replaced = preg_filter($patterns, $replacements, $subjects);
where:
$patterns: Regular express pattern, or array of patterns,
which are searched and replaced sequentially
$replacements: Replacement, or array of replacements,
which may contain $n or /n to refer to matches
$subjects: Subject, or array of subjects
$outputs: Subject, or array of subject after replacements
$replaced: Subjects that are affected by replacements
Here is an example script that searches and replaces expression patterns in a given string or array of strings.
<?php
# preg-replace-and-filter.php
# Copyright 2009-2024 (c) HerongYang.com. All Rights Reserved.
print("Test 1 - String arguments:\n");
$subject = "The quick brown fox jumps over the lazy dog";
$pattern = "/(fox)(.*)(dog)/i";
$replacement = "$3$2$1";
pcre_test($pattern, $replacement, $subject);
print("Test 2 - String Array arguments:\n");
$subjects = array(
"The quick brown fox jumps over the lazy dog",
"The quick brown cat jumps over the lazy dog",
"The quick black cat jumps over the fast dog"
);
$patterns = array(
"/(fox)(.*)(dog)/i",
"/brown/i",
"/lazy/i",
);
$replacements = array(
"$3$2$1",
"lazy",
"red",
);
pcre_test($patterns, $replacements, $subjects);
function pcre_test($patterns, $replacements, $subjects) {
print(" Patterns:\n"
.json_encode($patterns, JSON_PRETTY_PRINT)."\n");
print(" Replacements:\n"
.json_encode($replacements, JSON_PRETTY_PRINT)."\n");
print(" Original subjects:\n"
.json_encode($subjects, JSON_PRETTY_PRINT)."\n");
$outputs = preg_replace($patterns, $replacements, $subjects);
print(" Replaced subjects - affected and non-affected:\n"
.json_encode($outputs, JSON_PRETTY_PRINT)."\n");
$replaced = preg_filter($patterns, $replacements, $subjects);
print(" Replaced subjects - affected only:\n"
.json_encode($replaced, JSON_PRETTY_PRINT)."\n");
}
?>
If you run the above script, you will see the following output:
herong$ php preg-replace-and-filter.php
Test 1 - String arguments:
Patterns:
"\/(fox)(.*)(dog)\/i"
Replacements:
"$3$2$1"
Original subjects:
"The quick brown fox jumps over the lazy dog"
Replaced subjects - affected and non-affected:
"The quick brown dog jumps over the lazy fox"
Replaced subjects - affected only:
"The quick brown dog jumps over the lazy fox"
Test 2 - String array arguments:
Patterns:
[
"\/(fox)(.*)(dog)\/i",
"\/brown\/i",
"\/lazy\/i"
]
Replacements:
[
"$3$2$1",
"lazy",
"red"
]
Original subjects:
[
"The quick brown fox jumps over the lazy dog",
"The quick brown cat jumps over the lazy dog",
"The quick black cat jumps over the fast dog"
]
Replaced subjects - affected and non-affected:
[
"The quick red dog jumps over the red fox",
"The quick red cat jumps over the red dog",
"The quick black cat jumps over the fast dog"
]
Replaced subjects - affected only:
[
"The quick red dog jumps over the red fox",
"The quick red cat jumps over the red dog"
]
Note that:
Table of Contents
Introduction and Installation of PHP
Managing PHP Engine and Modules on macOS
Managing PHP Engine and Modules on CentOS
DOM Module - Parsing HTML Documents
GD Module - Manipulating Images and Pictures
MySQLi Module - Accessing MySQL Server
OpenSSL Module - Cryptography and SSL/TLS Toolkit
►PCRE Module - Perl Compatible Regular Expressions
preg_match() - Regular Expression Match
preg_grep() - Search Array Members
►preg_replace() - Replace Matched Substring
preg_split() - Split String into Tokens
preg_quote() - Escape Characters in Pattern
SOAP Module - Creating and Calling Web Services
SOAP Module - Server Functions and Examples