The preg_match() function in PHP is used to perform a regular expression match and return the number of matches. This function will stop the search after the first match. Function syntax: [int preg_match(string $pattern, string $subject)].
The preg_match function in PHP is used to perform a regular expression match.
(Recommended tutorial: php graphic tutorial)
Function syntax:
int preg_match(string $pattern ,string $subject[,array &$matches[,int $flags = 0[, int $offset = 0]]])
Parameters:
$pattern: The pattern to be searched, in string form.
$subject: Input string.
$matches: If the matches parameter is provided, it will be populated as search results. $matches[0] will contain the text matched by the full pattern, $matches[1] will contain the text matched by the first captured subgroup, and so on.
$flags: flags can be set to the following tag values:
PREG_OFFSET_CAPTURE: If this flag is passed, for each occurrence of the match The string offset (relative to the target string) is appended when returned.
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 matched string in the target string. Offset in subject.
offset: Normally, 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 bytes).
(Learning video recommendation: php video tutorial)
Return value:
Returns the number of matches for pattern. Its value will be 0 (no match) or 1 because preg_match() will stop searching after the first match.
Example:
<?php //模式分隔符后的"i"标记这是一个大小写不敏感的搜索 if (preg_match("/php/i", "PHP is the web scripting language of choice.")) { echo "查找到匹配的字符串 php。"; } else { echo "未发现匹配的字符串 php。"; } ?>
Output result:
查找到匹配的字符串 php。
The above is the detailed content of How to use the preg_match() function in php. For more information, please follow other related articles on the PHP Chinese website!