Regular Expressions (regex or regexp) are powerful tools for pattern matching and text manipulation in programming languages. PHP provides robust support for regular expressions, allowing developers to perform complex string operations with ease.
Getting Started with preg_match
The preg_match
function is a fundamental tool in PHP for matching a pattern in a string. Let’s look at a simple example:
<?php
$str = "Hello, World!";
if (preg_match("/Hello/", $str)) {
echo "Pattern found!";
} else {
echo "Pattern not found!";
}
?>
In this example, the pattern “/Hello/” is matched against the string, and if found, it prints “Pattern found!”.
Capturing Groups with preg_match
Capturing groups allow you to extract specific parts of a matched pattern. Consider the following example:
<?php
$str = "Date: 2022-01-01";
if (preg_match("/Date: (\d{4}-\d{2}-\d{2})/", $str, $matches)) {
echo "Found date: " . $matches[1];
} else {
echo "Date not found!";
}
?>
In this example, the pattern includes a capturing group (\d{4}-\d{2}-\d{2})
to extract the date, and it prints “Found date: 2022-01-01”.
Using preg_replace for Text Replacement
The preg_replace
function is handy for replacing text based on a pattern. Consider the following example:
<?php
$str = "I love apples and oranges.";
$newStr = preg_replace("/apples/", "bananas", $str);
echo $newStr;
?>
This code replaces “apples” with “bananas” in the original string, resulting in “I love bananas and oranges.”
Performing Case-Insensitive Matches
PHP regular expressions support modifiers to modify the behavior of the pattern matching. One common modifier is i
, which performs a case-insensitive match. Here’s an example:
<?php
$str = "Hello, world!";
if (preg_match("/HELLO/i", $str)) {
echo "Case-insensitive match found!";
} else {
echo "Case-insensitive match not found!";
}
?>
In this case, it will print “Case-insensitive match found!” as the i
modifier makes the pattern case-insensitive.
Handling Multiple Matches with preg_match_all
While preg_match
finds the first occurrence of a pattern, preg_match_all
is used to find all occurrences. Let’s explore an example:
<?php
$str = "The quick brown fox jumps over the lazy dog.";
if (preg_match_all("/\b\w{4}\b/", $str, $matches)) {
echo "Found words: " . implode(', ', $matches[0]);
} else {
echo "No words found!";
}
?>
This example matches all four-letter words in the given string and prints them.
Validating Email Addresses
Regular expressions are commonly used for validating email addresses. Here’s an example:
<?php
$email = "[email protected]";
if (preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email)) {
echo "Valid email address!";
} else {
echo "Invalid email address!";
}
?>
This pattern checks for a valid email address format.
Using Backreferences for Repetition
Backreferences allow you to refer to a captured group later in the pattern. Consider a simple example of matching repeated words:
<?php
$str = "The cat cat is cute.";
$newStr = preg_replace("/\b(\w+)\s+\1\b/", "$1", $str);
echo $newStr;
?>
This code replaces repeated words in the string, resulting in “The cat is cute.”
Handling Greedy and Non-Greedy Quantifiers
Quantifiers like *
and +
are greedy by default, matching as much as possible. Adding a ?
after a quantifier makes it non-greedy. Here’s an example:
<?php
$str = "<div>Content1</div><div>Content2</div>";
if (preg_match("/<div>(.*?)<\/div>/", $str, $matches)) {
echo "Content found: " . $matches[1];
} else {
echo "Content not found!";
}
?>
This non-greedy example extracts content from the first <div>
to the corresponding </div>
.