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:
Types of Regular Expressions in PHP
- POSIX-style regular expressions (using
eregfunctions, which are deprecated). - Perl-compatible regular expressions (PCRE) (using
pregfunctions).
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
1. Anchors
Anchors specify where a match should occur in a string.
Common Anchor Symbols
^asserts the position at the start of the string.$asserts the position at the end of the string.
Example
$pattern = "/^Hello/";
$string = "Hello world";
if (preg_match($pattern, $string)) {
echo "Starts with 'Hello'.";
}
Output
Starts with 'Hello'.
2. Character Classes
Character classes allow you to match one or more characters from a specified set.
Common Character Classes
[abc]matches any one of the characters inside the brackets.[^abc]matches any character not in the brackets.
Example
$pattern = "/[a-z]/";
$string = "Hello123";
if (preg_match($pattern, $string)) {
echo "Contains a lowercase letter.";
}
Output
Contains a lowercase letter.
3. Quantifiers
Quantifiers define how many times a character or pattern should appear.
Common Quantifiers
*matches 0 or more occurrences.+matches 1 or more occurrences.?matches 0 or 1 occurrence.{n}matches exactly n occurrences.
Example
$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
4. Groups and Capture
Groups allow you to capture parts of the matched text for later use.
Common Group Syntax
(abc)captures the substring "abc".\1refers to the first captured group.
Example
$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
5. Modifiers
Modifiers change how the regular expression engine performs matching.
Common Modifiers
imakes the pattern case-insensitive.mmakes^and$match the start and end of each line.smakes.match newline characters.
Example
$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
1. Replacing Text
You can replace matched text using the preg_replace() function.
$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
2. Splitting a String
You can split a string into an array using the preg_split() function.
$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.
)
Summary
Key Functions
preg_match()– Searches for a pattern in a string.preg_replace()– Replaces matched text.preg_split()– Splits a string using a regular expression.
Common Pattern Elements
- Anchors (
^,$) - Character classes (
[ ]) - Quantifiers (
*,+,?,{n}) - Groups and captures (
()) - Modifiers (
i,m,s)
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!
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
