Skip to content

Core\Ldap\Filter::startsWith

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

Use it for a search box that shows matches while a person types, or to find every account whose name starts with the same letters. 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. To find every entry that has the attribute at all, use Core\Ldap\Filter::present. A name with a character that an attribute name cannot have throws a LogicError too.

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

Search while typing

<?nvs
// Shows how to find the people whose name starts with what somebody typed so far.
string $typed = 'An';
var $filter = Core\Ldap\Filter::startsWith('displayName', $typed);
// This prints (displayName=An*).
echo $filter->toString(), "\n";
Output
(displayName=An*)

Only user accounts

<?nvs
// Shows how to find the user accounts whose login name starts with `svc-`.
var $filter = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::startsWith('sAMAccountName', 'svc-'),
);
// This prints (&(objectClass=user)(sAMAccountName=svc-*)).
echo $filter->toString(), "\n";
Output
(&(objectClass=user)(sAMAccountName=svc-*))

Empty text

<?nvs
// Shows what happens when the text to look for is empty.
string $typed = '';
try {
Core\Ldap\Filter::startsWith('displayName', $typed);
} catch (LogicError $error) {
// An empty text throws an error. `present` finds every entry with a value.
echo "Nothing typed yet.\n";
echo Core\Ldap\Filter::present('displayName')->toString(), "\n";
}
Output
Nothing typed yet.
(displayName=*)