Home > Backend Development > PHP Tutorial > PHP Regular Expressions: How to extract multiple substrings from a string

PHP Regular Expressions: How to extract multiple substrings from a string

WBOY
Release: 2023-06-23 09:00:02
Original
1291 people have browsed it

In PHP, regular expressions are a commonly used string matching tool. Through regular expressions, we can easily extract the required information from a string. This article will introduce how to use PHP regular expressions to extract multiple substrings from a string.

  1. preg_match_all function
    PHP provides the preg_match_all function, which can be used to find all substrings matching a certain pattern in a string. The syntax of this function is:
preg_match_all($pattern, $string, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0);
Copy after login

Among them, $pattern represents the regular expression that needs to be matched, $string represents the string that needs to be matched, and $matches is an array used to store all matched result. $flags represents the match flag, which can be PREG_PATTERN_ORDER or PREG_SET_ORDER. $offset represents the starting position of matching, which defaults to 0.

The following is a specific example, assuming that we need to match all numbers from a string:

$str = 'abc123def456ghi789jkl';
preg_match_all('/d+/', $str, $matches);
print_r($matches[0]);
Copy after login

The output result is:

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
)
Copy after login

As can be seen from the result , In the $matches array returned by the preg_match_all function, the first element represents the result matched by the entire regular expression, and the following elements represent the result corresponding to each bracket in the regular expression.

  1. Bracket Grouping
    In regular expressions, we can use brackets to group the content that needs to be matched. For example, we can use brackets to separate the numbers and letters that need to be matched, and then match them respectively:
$str = 'abc123def456ghi789jkl';
preg_match_all('/([a-z]+)(d+)/', $str, $matches);
print_r($matches);
Copy after login

The output result is:

Array
(
    [0] => Array
        (
            [0] => abc123
            [1] => def456
            [2] => ghi789
        )

    [1] => Array
        (
            [0] => abc
            [1] => def
            [2] => ghi
        )

    [2] => Array
        (
            [0] => 123
            [1] => 456
            [2] => 789
        )

)
Copy after login

As can be seen from the result, in the $matches array The first element of is the matching result of the entire regular expression, and the subsequent elements correspond to the matching results of each bracket in the regular expression. In this way, we can easily extract multiple substrings from the string.

  1. Non-greedy mode
    By default, regular expressions will use greedy mode for matching, that is, matching as many characters as possible. For example, for the string "aabbcc", we want to match "aa" and "bb", we can use the regular expression "/a. b/", but we will find that the matching result is "aabb", not what we expected" aa" and "bb".

To solve this problem, we can use non-greedy mode, which matches as few characters as possible. In regular expressions, use question marks to indicate non-greedy patterns. For example, to match "aa" and "bb" in the above example, you would use the regular expression "/a. ?b/".

$str = 'aabbcc';
preg_match_all('/a.+?b/', $str, $matches);
print_r($matches[0]);
Copy after login

The output result is:

Array
(
    [0] => aa
    [1] => bb
)
Copy after login

It can be seen from the results that the non-greedy mode can help us accurately match the required results.

Summary
This article describes how to use PHP regular expressions to extract multiple substrings from a string. Through the preg_match_all function, bracket grouping and non-greedy mode, we can flexibly deal with various situations and extract the required information. In actual development, using regular expressions can greatly improve the efficiency and success rate of string matching.

The above is the detailed content of PHP Regular Expressions: How to extract multiple substrings from a string. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template