函数:MongoDB\BSON\Regex::__construct()
适用版本:PHP 7, PHP 8
用法: MongoDB\BSON\Regex::__construct(string $pattern, string $flags = "")
说明: MongoDB\BSON\Regex::__construct()函数用于创建一个正则表达式对象。
参数:
示例:
$regex = new MongoDB\BSON\Regex('^hello', 'i');
$document = [
'name' => 'Hello World',
'description' => 'This is a demo.',
];
if (preg_match($regex, $document['name'])) {
echo 'Match found!';
} else {
echo 'No match found.';
}
在上面的示例中,我们创建了一个正则表达式对象,它匹配以"hello"开头的字符串,不区分大小写。然后,我们使用preg_match()函数来检查"Hello World"是否匹配该正则表达式。由于'i'标志的存在,匹配是不区分大小写的,因此输出为"Match found!"。