Skip to content

Core\Ldap\Filter::endsWith

Builds a filter that finds the directory entries where a value of an attribute ends with this text.

A common use is to find every person whose mail address is in one domain, such as all addresses that end with @example.test. 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 slower than equals or startsWith, because many servers cannot use an index for it.

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

Addresses in one domain

<?nvs
// Shows how to find every person whose mail address is in one domain.
var $filter = Core\Ldap\Filter::endsWith('mail', '@example.test');
// This prints (mail=*@example.test).
echo $filter->toString(), "\n";
Output
(mail=*@example.test)

One of two domains

<?nvs
// Shows how to find the mail addresses that end with one of two domains.
array<string> $domains = ['@example.test', '@shop.example.test'];
array<Core\Ldap\Filter> $parts = [];
foreach ($domains as string $domain) {
$parts[] = Core\Ldap\Filter::endsWith('mail', $domain);
}
// `any` takes the filters one by one, so `...` passes the array as separate values.
var $filter = Core\Ldap\Filter::any(...$parts);
// This prints (|(mail=*@example.test)(mail=*@shop.example.test)).
echo $filter->toString(), "\n";
Output
(|(mail=*@example.test)(mail=*@shop.example.test))

Special characters in the text

<?nvs
// Shows that special characters in the text are part of the value.
var $filter = Core\Ldap\Filter::endsWith('cn', '(old)');
// `(` and `)` are written as `\28` and `\29`.
// This prints (cn=*\28old\29).
echo $filter->toString(), "\n";
Output
(cn=*\28old\29)