MongoId::isValid

(PECL mongo >= 1.5.0)

MongoId::isValidCheck if a value is a valid ObjectId

说明

public static bool MongoId::isValid ( mixed $value )

This method may be used to check a variable before passing it as an argument to MongoId::__construct().

参数

value

The value to check for validity.

返回值

Returns TRUE if value is a MongoId instance or a string consisting of exactly 24 hexadecimal characters; otherwise, FALSE is returned.

User Contributed Notes

le6o 14-Jul-2016 11:57
There is no equivalent for this method in the new extension, so instead use...

<?php
if ($id instanceof \MongoDB\BSON\ObjectID
   
|| preg_match('/^[a-f\d]{24}$/i', $id)
) {
 
...
}
?>