PHP 5.6.29 Released

MongoId::isValid

(PECL mongo >= 1.5.0)

MongoId::isValidCheck if a value is a valid ObjectId

Description

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().

Parameters

value

The value to check for validity.

Return Values

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

add a note add a note

User Contributed Notes 1 note

up
3
le6o
4 months ago
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)
) {
 

}
?>
To Top