Home  >  Article  >  Backend Development  >  php preg_match will match how many times

php preg_match will match how many times

青灯夜游
青灯夜游Original
2022-03-21 20:01:231900browse

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

php preg_match will match how many times

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

php preg_match() function will match 1 time.

php preg_match() function

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

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

The parameter description is as follows:

  • $pattern: the pattern to be searched, which is the edited regular expression;

  • $subject: String to search for;

  • $matches: Optional parameter (array type), if $matches is provided, it will be populated as the search results. $matches[0] contains the text matched by the complete pattern, $matches[1] contains the text matched by the first capture subgroup, and so on;

  • $flags: Yes Optional parameters, $flags can be set to PREG_OFFSET_CAPTURE. If this flag is passed, for each occurrence of a match, the string offset (relative to the target string) will be appended to the return;

  • $offset: Optional parameter, used to specify where to start searching in the target string (unit is byte).

preg_match() function can return the number of matches for $pattern, its value will be 0 times (no match) or 1 time, because preg_match() will The search will stop.

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

<?php
    $subject = "abcdefghijkdef";
    $pattern_1 = &#39;/def/&#39;;
    $num = preg_match($pattern_1, $subject, $matches_1,PREG_OFFSET_CAPTURE,8);
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    var_dump($matches_1);
    var_dump($num);   //匹配次数为1次
    $pattern_2 = &#39;/def$/&#39;;
    $num = preg_match($pattern_2, $subject, $matches_2, PREG_OFFSET_CAPTURE, 3);
    var_dump($matches_2);
?>

php preg_match will match how many times

For the first match, the string will be The 8th bit searches for a substring that matches $pattern_1, and the $matches_2 array contains the matched substring and its position in the target string.

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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of php preg_match will match how many times. 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