Home >Backend Development >PHP Tutorial >PHP Regular Expressions: How to extract multiple substrings from a string
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.
preg_match_all($pattern, $string, &$matches, $flags = PREG_PATTERN_ORDER, $offset = 0);
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]);
The output result is:
Array ( [0] => 123 [1] => 456 [2] => 789 )
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.
$str = 'abc123def456ghi789jkl'; preg_match_all('/([a-z]+)(d+)/', $str, $matches); print_r($matches);
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 ) )
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.
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]);
The output result is:
Array ( [0] => aa [1] => bb )
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!