Skip to content

Core\Ldap\Filter::contains

Builds 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 as sAMAccountName. It cannot be tainted.
  • $valuestringThe text to look for. It may be tainted. A * in it is a normal character, and not a wildcard.

Return value

Returns Core\Ldap\Filter. A Core\Ldap\Filter.

Errors

  • LogicError$attribute is not an attribute name, or $value is 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";
Output
(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";
Output
(&(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";
Output
(displayName=*\2a*)