Skip to content

Core\Ldap\Dn::parse

Reads DN text, such as OU=Staff,DC=example,DC=test, and returns a Core\Ldap\Dn.

Use it for a DN that you wrote yourself, for example the base of your searches in a configuration file. The text cannot be tainted. To build a DN from user input, use Dn::of and child.

Good to know: spaces around ,, + and = are removed. Text that is not a DN throws a LogicError, and its message gives the position of the first wrong character.

Parameters

  • $textstringThe DN text, as RFC 4514 writes it. It cannot be tainted. Build a DN from user input with of and child.

Return value

Returns Core\Ldap\Dn. A Core\Ldap\Dn. Spaces around ,, + and = are removed.

Errors

  • LogicErrorThe text is not a DN. The message gives the position of the first wrong character.

Examples

A search base

<?nvs
// Shows how to read the base of your searches from text that you wrote.
// The spaces after each `,` are removed.
var $base = Core\Ldap\Dn::parse('OU=Staff, DC=example, DC=test');
// This prints OU=Staff,DC=example,DC=test.
echo $base->toString(), "\n";
Output
OU=Staff,DC=example,DC=test

An escaped comma

<?nvs
// Shows how a DN is read when a value has a comma in it.
// `\,` is a comma inside the value. It does not start a new part.
var $person = Core\Ldap\Dn::parse("CN=Lee\\, Ann,OU=Staff,DC=example,DC=test");
// This prints Lee, Ann.
echo $person->rdnValue(), "\n";
// This prints OU=Staff,DC=example,DC=test.
echo $person->parent()?->toString() ?? 'none', "\n";
Output
Lee, Ann
OU=Staff,DC=example,DC=test

Text that is not a dn

<?nvs
// Shows the error for text that is not a DN.
try {
// A `;` is not allowed in a value. It must be written as `\;`.
var $dn = Core\Ldap\Dn::parse('CN=Ann;DC=test');
echo $dn->toString(), "\n";
} catch (LogicError $error) {
// The message says that the text is wrong at character 7.
echo $error->message, "\n";
}
Output
Core\Ldap\Dn::parse: the text stops being a DN at character 7: a `;` in a value is written `\;`