PHP regular expressions
Sure! PHP regular expressions are patterns used to match strings within text. They are similar to regular expressions in other languages but have some syntax and function differences. PHP provides regular expression support through two different libraries:
- POSIX-style regular expressions (using
ereg
functions, which are deprecated). - Perl-compatible regular expressions (PCRE) (using
preg
functions).
Most modern PHP code uses PCRE due to its richer feature set and better performance. I'll focus on PCRE, which is accessed using functions like preg_match
, preg_replace
, and preg_split
.
Basic Syntax
A regular expression pattern is a string that describes a set of strings. Patterns are usually enclosed in delimiters, commonly slashes (/
).
Example 1: Simple Matching
$pattern = "/abc/";
$string = "abcdef";
if (preg_match($pattern, $string)) {
echo "Match found!";
} else {
echo "No match.";
}
Output:
Match found!
Here, /abc/
is a regular expression that looks for the substring "abc" in the string "abcdef".
Common Patterns and Functions
-
Anchors
^
asserts the position at the start of the string.$
asserts the position at the end of the string.
$pattern = "/^Hello/"; $string = "Hello world"; if (preg_match($pattern, $string)) { echo "Starts with 'Hello'."; }
Output:
Starts with 'Hello'.
-
Character Classes
[abc]
matches any one of the characters inside the brackets.[^abc]
matches any character not in the brackets.
$pattern = "/[a-z]/"; $string = "Hello123"; if (preg_match($pattern, $string)) { echo "Contains a lowercase letter."; }
Output:
Contains a lowercase letter.
-
Quantifiers
*
matches 0 or more occurrences.+
matches 1 or more occurrences.?
matches 0 or 1 occurrence.{n}
matches exactly n occurrences.
$pattern = "/\d+/"; // Matches one or more digits $string = "Phone number: 123456"; preg_match($pattern, $string, $matches); echo "First match: " . $matches[0];
Output:
First match: 123456
-
Groups and Capture
(abc)
captures the substring "abc".\1
refers to the first captured group.
$pattern = "/(Hello) (\w+)/"; $string = "Hello World"; if (preg_match($pattern, $string, $matches)) { echo "Match 1: " . $matches[1] . "\n"; // Hello echo "Match 2: " . $matches[2] . "\n"; // World }
Output:
Match 1: Hello Match 2: World
-
Modifiers
i
makes the pattern case-insensitive.m
makes^
and$
match the start and end of each line.s
makes.
match newline characters.
$pattern = "/hello/i"; // Case-insensitive $string = "Hello World"; if (preg_match($pattern, $string)) { echo "Case-insensitive match found!"; }
Output:
Case-insensitive match found!
Examples of More Advanced Usage
-
Replacing Text
$pattern = "/(\d{4})-(\d{2})-(\d{2})/"; $replacement = "$3/$2/$1"; $string = "2024-07-27"; $newString = preg_replace($pattern, $replacement, $string); echo $newString;
Output:
27/07/2024
-
Splitting a String
$pattern = "/[\s,]+/"; $string = "This is, a test string."; $result = preg_split($pattern, $string); print_r($result);
Output:
Array ( [0] => This [1] => is [2] => a [3] => test [4] => string. )
These examples cover some fundamental aspects of regular expressions in PHP. If you have specific patterns or use cases in mind, feel free to ask!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.