Core\Ldap\Filter::contains
Core\Ldap\Filter::contains(
string $attribute,
string $value,
): Core\Ldap\FilterBuilds a filter that finds the directory entries where a value of an attribute contains this text anywhere.
Use it when a person searches for a part of a name, a department or a description. The text is sent to the server as data. A * in it is a normal character and not a wildcard, so a value from a form cannot widen the search.
An empty text throws a LogicError, and so does a name with a character that an attribute name cannot have.
Good to know: a search like this is often slow on a large directory, because many servers cannot use an index for it. Combine it with a narrower filter in Core\Ldap\Filter::all where you can.
Parameters
$attributestringThe attribute's name, such assAMAccountName. It cannot betainted.$valuestringThe text to look for. It may betainted. A*in it is a normal character, and not a wildcard.
Return value
Returns Core\Ldap\Filter. A Core\Ldap\Filter.
Errors
LogicError$attributeis not an attribute name, or$valueis empty.
Examples
Part of a name
<?nvs// Shows how to find the people whose name contains a part that somebody typed.
string $typed = 'lee';var $filter = Core\Ldap\Filter::contains('displayName', $typed);// This prints (displayName=*lee*).echo $filter->toString(), "\n";(displayName=*lee*)Narrow it down
<?nvs// Shows how to search for a word in a description, only among groups.
var $filter = Core\Ldap\Filter::all( Core\Ldap\Filter::equals('objectClass', 'group'), Core\Ldap\Filter::contains('description', 'printer'),);// The narrow filter comes first, so a person reading the log sees it first.// This prints (&(objectClass=group)(description=*printer*)).echo $filter->toString(), "\n";(&(objectClass=group)(description=*printer*))A star in the text
<?nvs// Shows that a `*` in the text does not match everything.
string $typed = '*';var $filter = Core\Ldap\Filter::contains('displayName', $typed);// The `*` is a normal character, written as `\2a`. Only names with a `*` match.// This prints (displayName=*\2a*).echo $filter->toString(), "\n";(displayName=*\2a*)