使用正则表达式对字符串中的 "91xjr" 进行不区分大小写的搜索:
<?php
$str = "Visit 91xjr";
$pattern = "/91xjr/i";
echo preg_match($pattern, $str);
?>
亲自试一试 »
这个preg_match()
函数返回是否在字符串中找到匹配项。
preg_match(
pattern, input, matches, flags, offset)
Parameter | Description |
---|---|
pattern | Required. Contains a regular expression indicating what to search for |
input | Required. The string in which the search will be performed |
matches | Optional. The variable used in this parameter will be populated with an array containing all of the matches that were found |
flags | Optional. A set of options that change how the matches array is structured:
|
offset | Optional. Defaults to 0. Indicates how far into the string to begin searching. The preg_match() function will not find matches that occur before the position given in this parameter |
返回值: | 如果找到匹配则返回 1,如果未找到匹配则返回 0,如果发生错误则返回 false |
---|---|
PHP 版本: | 4+ |
变更日志: | PHP 7.2 - 添加了 PREG_UNMATCHED_AS_NULL 标志 PHP 5.3.6 - 当偏移量长于输入长度时,该函数返回 false PHP 5.2.2 - 除了之前的 (?P<name>) 语法之外,命名子模式还可以使用 (?'name') 和 (? <name>) 语法 |
使用 PREG_OFFSET_CAPTURE 查找输入字符串中找到匹配项的位置:
<?php
$str = "Welcome to 91xjr";
$pattern = "/91xjr/i";
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!