Master PHP Regex: How to Use preg_match, preg_replace, and More
This guide explains PHP’s regular expression functions—including preg_match, preg_match_all, preg_replace, and preg_split—detailing their syntax, required parameters, and practical code examples, while also covering advanced regex meta‑characters and symbols to enhance string matching and data processing.
Regular expressions are powerful data‑matching tools that efficiently describe patterns in strings. In PHP, regex functions provide flexible data processing and filtering capabilities.
Basic Syntax of Regular Expressions
Regular expressions consist of characters and special symbols. Common PHP regex functions include preg_match(), preg_match_all(), preg_replace() and preg_split().
preg_match() function
preg_match()performs pattern matching on a string and returns a boolean result—true if a match is found, false otherwise.
It requires three parameters:
Regular expression pattern
String to be searched
Variable to store the match results
Example:
$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$flag = preg_match($pattern, $str, $matches);
if($flag){
echo 'Match succeeded!';
print_r($matches);
} else {
echo 'Match failed!';
}The pattern /php/i matches the substring "php" case‑insensitively, so the match succeeds.
preg_match_all() function
preg_match_all()finds all occurrences of a pattern in a string and returns the results.
It also requires three parameters (pattern, string, matches variable) and works similarly to preg_match().
$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$flag = preg_match_all($pattern, $str, $matches);
if($flag){
echo 'Match succeeded!';
print_r($matches);
} else {
echo 'Match failed!';
}Since "php" appears twice, the function returns both matches.
preg_replace() function
preg_replace()replaces matches of a pattern with a replacement string.
It requires three parameters: pattern, subject string, and replacement string.
$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$replace = 'JavaScript';
$new_str = preg_replace($pattern, $replace, $str);
echo $new_str; // Learn JavaScript, it's easy!preg_split() function
preg_split()splits a string by a regular‑expression pattern and returns an array.
It requires two parameters: the pattern and the string to split.
$pattern = '/,/' ;
$str = 'apple,banana,orange';
$arr = preg_split($pattern, $str);
print_r($arr); // ['apple','banana','orange']Advanced Regex Features
Beyond basic pattern matching, regular expressions provide many advanced features such as meta‑characters and special symbols.
Meta‑characters
. matches any character except a newline
^ matches the start of the string
$ matches the end of the string
* matches the preceding element zero or more times
+ matches the preceding element one or more times
? matches the preceding element zero or one time
{n} matches exactly n occurrences
{n,m} matches between n and m occurrences
Special symbols
\d matches any digit
\D matches any non‑digit
\w matches any word character (letter, digit, underscore)
\W matches any non‑word character
\s matches any whitespace character
\S matches any non‑whitespace character
Combining these meta‑characters and symbols greatly enhances regex matching capabilities for data filtering and processing.
Using PHP’s regex functions enables powerful data matching, allowing more flexible string handling, improving development efficiency, and increasing code extensibility.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
