Skip to content

Core\Ldap\Dn::toString

$dn->toString(): tainted string

Returns the DN as text, such as CN=Ann,OU=Staff,DC=example,DC=test.

Use it to write a DN to a log or to show it on a page. Each special character in a value is escaped, so Lee, Ann is written as Lee\, Ann.

Good to know: the result is tainted, because a value in the DN can come from user input. Spaces around , and = are not in the text.

Return value

Returns tainted string. The text. Dn::parse reads it back as the same DN. The text is tainted, because a value in the DN may be.

Changelog

VersionChange
0.0.6Throwable::$message is tainted string, because some error messages contain input text. The text of a Core\Ldap\Filter and of a Core\Ldap\Dn is tainted string too.

Examples

Write to a log

<?nvs
// Shows how to write a DN that you built as text.
var $user = Core\Ldap\Dn::parse('OU=Staff,DC=example,DC=test')->child('CN', 'Ann');
// This prints new user: CN=Ann,OU=Staff,DC=example,DC=test.
echo 'new user: ', $user->toString(), "\n";
Output
new user: CN=Ann,OU=Staff,DC=example,DC=test

Special characters

<?nvs
// Shows how special characters in a value are written in the text.
// A `,`, a `+` and a space at the start each get a `\` in front.
array<string> $names = ['Lee, Ann', 'A+B', ' Ann'];
// This prints CN=Lee\, Ann, then CN=A\+B, then CN=\ Ann.
foreach ($names as string $name) {
echo Core\Ldap\Dn::of('CN', $name)->toString(), "\n";
}
Output
CN=Lee\, Ann
CN=A\+B
CN=\ Ann

Spaces are removed

<?nvs
// Shows that the text keeps the case you wrote, without the spaces around `,` and `=`.
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