MongoDB\BSON\Regex::__construct

(mongodb >=1.0.0)

MongoDB\BSON\Regex::__constructConstruit une nouvelle REGEX

Description

final public MongoDB\BSON\Regex::__construct(string $pattern, string $flags = "")

Liste de paramètres

pattern (string)

Le masque de l'expression régulière.

Note: Le masque ne doit pas être entouré de caractères délimitants.

flags (string)

Les »  drapeaux de l'expression régulière.

Erreurs / Exceptions

Historique

Version Description
PECL mongodb 1.2.0

L'arguement flags est optionnel et la valeur par défaut est une chaîne vide.

Les caractères dans l'argument flags seront triés par ordre alphabétique lorsqu'une Regex est construite. Auparavant, les caractères étaient stockés dans l'ordre fourni.

MongoDB\Driver\Exception\InvalidArgumentException est livé si pattern ou flags contient un octet nul. Auparavant, les valeurs étaient tronquées au premier octet nul.

Exemples

Exemple #1 MongoDB\BSON\Regex::__construct() example

<?php

$regex
= new MongoDB\BSON\Regex('^foo', 'i');
var_dump($regex);

?>

L'exemple ci-dessus va afficher :

object(MongoDB\BSON\Regex)#1 (2) {
  ["pattern"]=>
  string(4) "^foo"
  ["flags"]=>
  string(1) "i"
}
add a note

User Contributed Notes 1 note

up
4
Alejandro Wilcke
4 years ago
This matches with any fieldName that includes the string:
$mongoRegex = new MongoDB\BSON\Regex("$string", "i");

This matches with any fieldName that STARTS with the string:
$mongoRegex = new MongoDB\BSON\Regex("^$string", "i");

$cursor = $collection->find( [ 'fieldName' => $mongoRegex ] );

$docs = [];

foreach($cursor as $doc){
$docs[] = $doc;
}

return $docs;
To Top