Using PHP preg_replace() for String Replacement: Syntax, Examples, and Tips

This article explains the PHP preg_replace() function, its basic syntax, and provides multiple code examples showing how to replace specific characters, optional characters, digits, perform case‑insensitive replacements, and mask email addresses using regular expressions.

php Courses
php Courses
php Courses
Using PHP preg_replace() for String Replacement: Syntax, Examples, and Tips

In PHP, the preg_replace() function is a powerful and flexible tool that allows searching and replacing strings using regular expressions, enabling tasks such as removing specific characters or substituting patterned text.

Basic Syntax of preg_replace()

preg_replace(pattern, replacement, subject);

The pattern is the regular expression to match, replacement is the string to replace matches with, and subject is the target string on which the operation is performed.

Below are concrete examples demonstrating how to use preg_replace() for various replacement scenarios.

Replacing Specific Characters in a String

Given a string containing the character “#”, the following code replaces all “#" with “_”.

$str = "Hello#World#";
$newStr = preg_replace('/#/', '_', $str);
echo $newStr; // Output: Hello_World_

Replacing Multiple Optional Characters

This example replaces any occurrence of “a”, “b”, or “c” with “x”.

$str = "abcde";
$newStr = preg_replace('/[abc]/', 'x', $str);
echo $newStr; // Output: xxxde

Replacing Digits in a String

To replace all digits with “*”, the \d token is used.

$str = "abc123def456";
$newStr = preg_replace('/\d/', '*', $str);
echo $newStr; // Output: abc***def***

Case‑Insensitive Replacement

Adding the /i modifier makes the pattern case‑insensitive.

$str = "Hello World";
$newStr = preg_replace('/world/i', 'PHP', $str);
echo $newStr; // Output: Hello PHP

Replacing Email Addresses

This example masks email addresses using a regular expression that matches typical email patterns.

$str = "My email is [email protected]";
$newStr = preg_replace('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/', '***', $str);
echo $newStr; // Output: My email is ***

These examples illustrate basic uses of preg_replace(); the function also offers many additional options and features for advanced string manipulation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Tutorialregexstring-replacementpreg_replace
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.