Home  >  Article  >  Backend Development  >  What is the usage of php preg match?

What is the usage of php preg match?

coldplay.xixi
coldplay.xixiOriginal
2020-09-30 09:42:521944browse

php preg match usage is to perform a regular expression match, the syntax is [int preg_match (string $pattern, string $subject [, array &$matches [, int $flags.]]].

What is the usage of php preg match?

preg_match The function is used to perform a regular expression match.

Syntax

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Search for a match between subject and the regular expression given by pattern.

Parameter description:

  • $pattern: the pattern to be searched, String form.

  • $subject: Input string.

  • $matches: If the parameter matches is provided, it will be filled in for the search The result. $matches[0] will contain the text matched by the full pattern, $matches[1] will contain the text matched by the first capturing subgroup, and so on.

  • $flags: flags can be set to the following flag values:

    PREG_OFFSET_CAPTURE: If this flag is passed, the string offset (relative to the target string) will be returned for each occurrence of a match. Note: This will change the array filled in the matches parameter so that each element becomes a string where the 0th element is the matched string and the 1st element is the offset of the matched string in the target string subject. .

  • offset: Usually, the search starts from the beginning of the target string. The optional parameter offset is used to specify the search starting from an unknown point in the target string (unit is byte) .

Return value

Returns the number of matches for pattern. Its value will be 0 times (no match) or 1 time, because preg_match () will stop the search after the first match. Unlike preg_match_all(), it will search the subject until it reaches the end. If an error occurs preg_match() returns FALSE.

Example

1. Search for the text string "php":

The execution result is as follows:

查找到匹配的字符串 php。

2. Search for the word "word"

The execution result is as follows:

查找到匹配的字符串。
未发现匹配的字符串。

3. Get the domain name in the URL

The execution result is as follows:

domain name is: runoob.com

4. Use named subgroups

\w+): (?P\d+)/', $str, $matches);
 
/* 下面例子在php 5.2.2(pcre 7.0)或更新版本下工作, 然而, 为了后向兼容, 上面的方式是推荐写法. */
// preg_match('/(?\w+): (?\d+)/', $str, $matches);
 
print_r($matches);
 
?>

The execution results are as follows:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

If you want to learn more about programming, please pay attention to the php training column!

The above is the detailed content of What is the usage of php preg match?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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