Backend Development 7 min read

PHP Regular Expression Functions: Syntax, Usage, and Examples

This article explains PHP's regular expression functions—including preg_match, preg_match_all, preg_replace, and preg_split—detailing their syntax, parameters, and practical code examples, and also covers advanced regex metacharacters and special symbols for powerful string matching and manipulation.

php中文网 Courses
php中文网 Courses
php中文网 Courses
PHP Regular Expression Functions: Syntax, Usage, and Examples

Regular expressions are a powerful data‑matching tool that can efficiently match patterns in strings. In PHP, regular‑expression functions provide many capabilities, making data processing and filtering more flexible and convenient.

Basic Syntax of Regular Expressions

Regular expressions consist of a series of characters and special symbols that describe string patterns. In PHP, the commonly used functions include preg_match() , preg_match_all() , preg_replace() and preg_split() .

preg_match() Function

The preg_match() function performs a regular‑expression match on a string and returns a boolean result: true if the match succeeds, otherwise false .

Using preg_match() requires three arguments:

Regular‑expression pattern

The string to be searched

A variable to store the match results

Example:

<code>$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$flag = preg_match($pattern, $str, $matches);
if($flag){
    echo '匹配成功!';
    print_r($matches);
} else {
    echo '匹配失败!';
}
</code>

In the example, the pattern /php/i (case‑insensitive) matches the substring "php" in the target string, so the match succeeds and the result is printed.

preg_match_all() Function

The preg_match_all() function finds all occurrences of a pattern in a string and returns the matches.

It also requires three arguments: pattern, subject string, and a variable for the matches.

Example:

<code>$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$flag = preg_match_all($pattern, $str, $matches);
if($flag){
    echo '匹配成功!';
    print_r($matches);
} else {
    echo '匹配失败!';
}
</code>

Because "php" appears twice, preg_match_all() returns both matches.

preg_replace() Function

The preg_replace() function replaces parts of a string that match a regular‑expression pattern.

It requires three arguments: pattern, the string to be replaced, and the replacement string.

Example:

<code>$pattern = '/php/i';
$str = 'Learn PHP, it\'s easy!';
$replace = 'JavaScript';
$new_str = preg_replace($pattern, $replace, $str);
echo $new_str;
</code>

The pattern /php/i is replaced by "JavaScript", producing the output "Learn JavaScript, it's easy!".

preg_split() Function

The preg_split() function splits a string by a regular‑expression pattern and returns an array.

It requires two arguments: the pattern and the string to split.

Example:

<code>$pattern = '/,/';
$str = 'apple,banana,orange';
$arr = preg_split($pattern, $str);
print_r($arr);
</code>

The pattern /, / splits the string into the array ['apple', 'banana', 'orange'] .

Advanced Features for Data Matching with Regular Expressions

Beyond basic pattern matching, regular expressions offer many advanced features such as metacharacters and special symbols.

Metacharacters

'.' – matches any single character except 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 the preceding element exactly n times.

'{n,m}' – matches the preceding element at least n times, at most m times.

Special Symbols

'\d' – matches any digit.

'\D' – matches any non‑digit character.

'\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 metacharacters and special symbols greatly enhances the matching power of regular expressions, making data filtering and processing much more efficient.

Mastering PHP's regular‑expression functions enables developers to perform robust string matching and manipulation, improving development efficiency and code extensibility.

Backend Developmentphpregular expressionsregexstring processingpreg_match
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.