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.
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 = '/def/'; $num = preg_match($pattern_1, $subject, $matches_1,PREG_OFFSET_CAPTURE,8); echo '<pre class="brush:php;toolbar:false">'; var_dump($matches_1); var_dump($num); //匹配次数为1次 $pattern_2 = '/def$/'; $num = preg_match($pattern_2, $subject, $matches_2, PREG_OFFSET_CAPTURE, 3); var_dump($matches_2); ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
