Get the App
SLTechnology News&Howtos  ›  Development  › 

How many times will preg_match match in php?

Shulou Source: shulou.com Published: 2022-06-01 04:25:59 09月30日 Update

This article mainly introduces how many times the preg_match will match in php. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!

The php preg_match () function matches once. The preg_match () function is used to search for matches on strings based on regular expressions, and can return the number of matches; the value of preg_match () will be 0 (mismatch) or 1, because it will stop searching after the first match.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The php preg_match () function matches once.

Php preg_match () function

The preg_match () function in PHP can search and match strings based on regular expressions. The syntax format of the function is as follows:

Preg_match ($pattern,$subject [, & $matches [, $flags = 0 [, $offset = 0])

The parameters are described as follows:

$pattern: the pattern to search for, that is, the edited regular expression

$subject: string to search for

$matches: optional parameter (array type), which will be populated with search results if $matches is provided. $matches [0] contains the text to which the full pattern matches, $matches [1] contains the text matched by the first capture subgroup, and so on

$flags: optional parameter. $flags can be set to PREG_OFFSET_CAPTURE. If this flag is passed, the string offset (relative to the target string) will be appended to the return for each occurrence of the match.

$offset: optional parameter that specifies where to start the search (in bytes) in the target string.

The preg_match () function can return the number of matches for $pattern, and its value will be 0 (mismatch) or 1, because preg_match () will stop searching after the first match.

Example: use the preg_match () function to search for a string

For the first match, the substring matching $pattern_1 is searched from the 8th bit of the string, and the $matches_2 array contains the matching substring and its location in the target string.

Note that the second regular expression is different from the first regular expression by adding a positioning symbol $to indicate the position at the end of the matching string.

The above is all the content of the article "how many times will preg_match match in php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Character string search function regular expression parameter first time position number goal content array text pattern article different and so on that is value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Apple Xiaomi Shulou Technology