Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Replacing Patterns in PHP

Introduction

Replacing patterns in strings is a common task in programming, and PHP offers a powerful tool for this purpose: regular expressions. Regular expressions, or regex, are patterns used to match character combinations in strings. PHP provides the preg_replace function to search a string for a pattern and replace it with another string.

Basic Syntax of preg_replace

The preg_replace function in PHP is used to perform a regular expression search and replace. Here is the basic syntax:

preg_replace(pattern, replacement, subject);

- pattern: The regular expression pattern to search for.
- replacement: The string to replace the pattern with.
- subject: The input string.

Example 1: Replacing a Simple Pattern

Let's start with a simple example where we replace all occurrences of the word "cat" with "dog" in a given string.

<?php
$subject = "The cat sat on the mat.";
$pattern = "/cat/";
$replacement = "dog";
$result = preg_replace($pattern, $replacement, $subject);
echo $result;
?>
The dog sat on the mat.

Example 2: Replacing Multiple Patterns

You can also replace multiple patterns at once by passing arrays to the preg_replace function. Here, we will replace "cat" with "dog" and "mat" with "rug".

<?php
$subject = "The cat sat on the mat.";
$patterns = ["/cat/", "/mat/"];
$replacements = ["dog", "rug"];
$result = preg_replace($patterns, $replacements, $subject);
echo $result;
?>
The dog sat on the rug.

Example 3: Using Modifiers

Regular expressions can be modified using various modifiers. For example, the "i" modifier makes the pattern case-insensitive. Here, we will replace "Cat" with "dog" in a case-insensitive manner.

<?php
$subject = "The Cat sat on the mat.";
$pattern = "/cat/i";
$replacement = "dog";
$result = preg_replace($pattern, $replacement, $subject);
echo $result;
?>
The dog sat on the mat.

Example 4: Using Subpatterns

Subpatterns, or capturing groups, allow you to capture parts of the pattern and use them in the replacement. Here, we will capture the word "cat" and replace it with "happy cat".

<?php
$subject = "The cat sat on the mat.";
$pattern = "/(cat)/";
$replacement = "happy $1";
$result = preg_replace($pattern, $replacement, $subject);
echo $result;
?>
The happy cat sat on the mat.

Example 5: Escaping Special Characters

Some characters have special meanings in regular expressions. To use these characters as literals, you need to escape them with a backslash. Here, we will replace the period character "." with an exclamation mark "!".

<?php
$subject = "Hello. How are you?";
$pattern = "/\./";
$replacement = "!";
$result = preg_replace($pattern, $replacement, $subject);
echo $result;
?>
Hello! How are you?

Conclusion

Replacing patterns using regular expressions in PHP is a powerful and flexible way to manipulate strings. By understanding the syntax and various options available, you can perform complex text processing tasks with ease. Practice with different patterns and modifiers to become proficient in using preg_replace.