PHP Velho Oeste 2024

MongoRegex クラス

(PECL mongo >=0.8.1)

警告

このクラスを定義している拡張モジュールは非推奨です。 かわりに MongoDB 拡張モジュールを使うべきです。 このクラスの代替として、以下が使えます。

はじめに

このクラスを使うと正規表現を作ることができます。 典型的な使い道は、データベースへの問い合わせでマッチする文字列を検索することです。 それ以外に、正規表現をデータベースに格納したりデータベースから取得したりすることもできます。

正規表現は、四つの部分で構成されています。最初は区切り文字の /、 そしてその後にパターンが続き、さらにもう一度区切り文字の /、 そして最後がフラグを表す文字です。

例1 正規表現のパターン

/pattern/flags

Mongo は 6 つの正規表現フラグに対応しています。

  • i: 大文字小文字を区別しない

  • m: 複数行

  • x: コメントを含めることができる

  • l: ロケール

  • s: "." が、改行を含むすべてにマッチする

  • u: unicode にマッチする

クラス概要

MongoRegex {
/* フィールド */
public string regex ;
public string flags ;
/* メソッド */
public __construct ( string $regex )
public __toString ( void ) : string
}

目次

add a note add a note

User Contributed Notes 3 notes

up
3
vinicius at codemakers dot com dot br
10 years ago
/*
Use the "i" option to make searches with Case Insensitive
Find results beginning with $q
*/

$search = "V";

// Case Sensitive

$where = array('name' => array('$regex' => new MongoRegex("/^$search/")));
$cursor = $collection->find($where);

//Case Insensitive

$where = array('name' => array('$regex' => new MongoRegex("/^$search/i")));
$cursor = $collection->find($where);
up
2
thusitha555 at gmail dot com
9 years ago
here is an example for case insensitive search.If there are two fields (user_name/company_name) in collection;
<?php
$search_string
='baR';
$searchQuery = array(
           
'$or' => array(
                array(
                   
'user_name' => new MongoRegex("/^$search_string/i"),
                    ),
                array(
                   
'company_name' => new MongoRegex("/^$search_string/i"),
                    ),
                )
            );

$cursor = $customers->find($searchQuery);
?>
up
0
benyounes dot ousama at gmail dot com
13 years ago
First you must declare and define your regexObj
Here I am looking for all entry of my database wich is like "%Nicolas%" and the /i param is used for Insensitive Case
$regexObj = new MongoRegex("/^Nicolas/i");

<?php
// I attach the regexObj to my Where Condition
$where = array("ctname" => $regexObj);

// Execute the request
$resultset = $this->db->Infos->find($where);

// Parsing the results
while ($resultset->hasNext())
{
        
$clientObj = $resultset->getNext();
          echo
"Client Name: ".$clientObj["cname"]."</br>";
}
?>
To Top