Skip to content

Core\Ldap\Filter::approximately

Builds a filter that finds the directory entries where a value of an attribute is close to this value.

Each server has its own rules for what "close" means. Many servers compare how names sound, so Smyth can find Smith. Some servers treat it the same as Core\Ldap\Filter::equals. Test it against your own server before you depend on it.

The value is sent as data, so it may come from a form. An attribute name with a character that is not allowed in a name throws a LogicError.

Parameters

  • $attributestringThe attribute's name, such as sAMAccountName. It cannot be tainted.
  • $valuestringThe value to compare with. It may be tainted.

Return value

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

Errors

  • LogicError$attribute is not an attribute name.

Changelog

VersionChange
0.0.6Core\Ldap\Connection->hasValue() checks whether an entry has a value, and searchChanges() reads the entries that changed. Core\Ldap\Filter::approximately builds an approximate match. The old names were compare, changes and approx.

Examples

A name that sounds the same

<?nvs
// Shows how to find a family name that may be written in a different way.
var $filter = Core\Ldap\Filter::approximately('sn', 'Smyth');
// The server decides which names are close. This prints (sn~=Smyth).
echo $filter->toString(), "\n";
Output
(sn~=Smyth)

Exact or close

<?nvs
// Shows how to find a name that is written exactly so, or is close to it.
string $typed = 'Meyer';
var $filter = Core\Ldap\Filter::any(
Core\Ldap\Filter::equals('sn', $typed),
Core\Ldap\Filter::approximately('sn', $typed),
);
// This prints (|(sn=Meyer)(sn~=Meyer)).
echo $filter->toString(), "\n";
Output
(|(sn=Meyer)(sn~=Meyer))

A name from a form

<?nvs
// Shows that a value with special characters stays one value.
// Think of this text as a name that somebody typed into a form.
string $typed = 'Lee)(cn=*';
var $filter = Core\Ldap\Filter::approximately('sn', $typed);
// `(`, `)` and `*` are part of the value. This prints (sn~=Lee\29\28cn=\2a).
echo $filter->toString(), "\n";
Output
(sn~=Lee\29\28cn=\2a)