PHPackages                             paweldecowski/ldap-dn - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. paweldecowski/ldap-dn

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

paweldecowski/ldap-dn
=====================

Parse and manipulate LDAP Distinguished Names

v1.0(5y ago)99.8k↓50%1MITPHPPHP &gt;=7.1

Since Mar 17Pushed 5y ago1 watchersCompare

[ Source](https://github.com/PawelDecowski/ldap-dn)[ Packagist](https://packagist.org/packages/paweldecowski/ldap-dn)[ RSS](/packages/paweldecowski-ldap-dn/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

LDAP DN
=======

[](#ldap-dn)

LDAP Distinguished Name parsing and manipulation library for PHP.

[![Packagist Version](https://camo.githubusercontent.com/269607baead3b0af46f433f6f34420d65a478fa0bcf80f6835fb9f8858f69738/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706177656c6465636f77736b692f6c6461702d646e)](https://camo.githubusercontent.com/269607baead3b0af46f433f6f34420d65a478fa0bcf80f6835fb9f8858f69738/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706177656c6465636f77736b692f6c6461702d646e)[![Travis (.com)](https://camo.githubusercontent.com/40faf04aa14bee219f9b1be73d34befb70595e5bfb1459b31bd75c7bd831f41c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f506177656c4465636f77736b692f6c6461702d646e)](https://camo.githubusercontent.com/40faf04aa14bee219f9b1be73d34befb70595e5bfb1459b31bd75c7bd831f41c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f506177656c4465636f77736b692f6c6461702d646e)[![Coveralls](https://camo.githubusercontent.com/1055a0c5766c84b7b50aca67b10cbcbb2b6b84862ecafce8fe40d570c92570e2/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f506177656c4465636f77736b692f6c6461702d646e)](https://camo.githubusercontent.com/1055a0c5766c84b7b50aca67b10cbcbb2b6b84862ecafce8fe40d570c92570e2/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f506177656c4465636f77736b692f6c6461702d646e)[![GitHub](https://camo.githubusercontent.com/b391015c2034b394bfea683c20d4ad4e235c3e449fc5ae9db3937e07eb793d4c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f506177656c4465636f77736b692f6c6461702d646e)](https://camo.githubusercontent.com/b391015c2034b394bfea683c20d4ad4e235c3e449fc5ae9db3937e07eb793d4c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f506177656c4465636f77736b692f6c6461702d646e)

Table of contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [How to use](#how-to-use)
    - [Parsing DNs and accessing their components](#parsing-dns-and-accessing-their-components)
        - [Create a DN object from a string](#create-a-dn-object-from-a-string)
        - [Back to string representation](#back-to-string-representation)
        - [Access RDNs by index](#access-rdns-by-index)
        - [Iterate RDNs](#iterate-rdns)
        - [Access attributes](#access-attributes)
        - [Multivalued RDNs](#multivalued-rdns)
        - [Special characters](#special-characters)
        - [Filter by attribute name](#filter-by-attribute-name)
        - [Get the parent DN](#get-the-parent-dn)
    - [Manipulating DNs](#manipulating-dns)
        - [Remove a fragment of a DN](#remove-a-fragment-of-a-dn)
    - [Constructing DNs](#constructing-dns)
    - [Exceptions](#exceptions)

Features
--------

[](#features)

- access individual RDNs by index
- iterate RDNs
- filter by attribute name
- support for escaping special characters
- access attribute values
- remove fragments of a DN
- construct DNs
- case-insensitive but case-preserving (lookups are case-insensitive but attribute names’ and values’ case is preserved)
- 100% test coverage

Requirements
------------

[](#requirements)

- PHP 7.1

Installation
------------

[](#installation)

```
$ composer require paweldecowski/ldap-dn
```

How to use
----------

[](#how-to-use)

### Parsing DNs and accessing their components

[](#parsing-dns-and-accessing-their-components)

#### Create a DN object from a string

[](#create-a-dn-object-from-a-string)

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');
```

#### Back to string representation

[](#back-to-string-representation)

`Dn` class as well as classes representing its components (`Rdn`, `Attribute`) implement the `__toString` method. It means that in string context, they automatically become strings:

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn; // 'cn=john.doe,ou=it,ou=leadership,dc=example,dc=org'
echo $dn[0]; // 'dc=org'
echo $dn->filter('ou'); // 'ou=it,ou=leadership'
```

#### Access RDNs by index

[](#access-rdns-by-index)

Note that `Rdn`s in a `Dn` object are reversed in relation to the DN string (if read from left to right). In a `Dn` with `n` `Rdn`s, the right-most `Rdn` is at index `0` and the left-most `Rdn` is at index `n-1`. This is because it’s more natural and common to have the root object at index `0`.

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn[0]; // 'dc=org'
echo $dn[4]; // 'cn=john.doe'
```

#### Iterate RDNs

[](#iterate-rdns)

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

foreach ($dn as $rdn) {
    echo $rdn, "\n";
}
```

outputs:

```
dc=org
dc=example
ou=leadership
ou=it
cn=john.doe

```

#### Access attributes

[](#access-attributes)

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn[0]['dc']->getValue(); // 'org'
echo $dn[4]['cn']->getValue(); // 'john.doe'
```

If there’s only one instance of a certain attribute, you can get its value directly from the `Dn` object:

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn->getValue('cn'); // 'john.doe'
```

If mulitiple attributes with the specified name are found, a `MultipleAttributesReturnedException` is thrown.

#### Multivalued RDNs

[](#multivalued-rdns)

`Rdn`s are allowed to have multiple attributes, separated by the `+` character. You can access them using array dereferencing syntax.

```
$dn = LdapDn\Dn::fromString('cn=john.doe+uid=123,ou=it,ou=leadership,dc=example,dc=org');

echo $dn[4]; // 'cn=john.doe+uid=123'
echo $dn[4]['cn']; // 'cn=john.doe'
echo $dn[4]['cn']->getValue(); // 'john.doe'
echo $dn[4]['uid']; // 'uid=123'
echo $dn[4]['uid']->getValue(); // '123'
```

You can also iterate attributes if you don’t know their names.

```
$dn = LdapDn\Dn::fromString('cn=john.doe+uid=123,ou=it,ou=leadership,dc=example,dc=org');

foreach ($dn[4] as $attribute) {
    echo $attribute->getName(), ' is ', $attribute->getValue(), "\n";
}
```

outputs:

```
cn is john.doe
uid is 123

```

#### Special characters

[](#special-characters)

```
$dn = LdapDn\Dn::fromString('cn=doe\, john,ou=it,ou=leadership,dc=example,dc=org');

echo $dn[4]; // 'cn=doe\, john'
echo $dn[4]['cn']->getValue(); // 'doe, john'
```

#### Filter by attribute name

[](#filter-by-attribute-name)

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn->filter('ou'); // 'ou=it,ou=leadership'
```

Note that even though the result of `filter()` is a `Dn` object, it may not be a valid Distinguished Name (for example if you remove the root RDN). This library doesn’t have the knowledge of your LDAP structure, so it can’t ensure validity.

#### Get the parent DN

[](#get-the-parent-dn)

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');

echo $dn->getParent(); // 'ou=it,ou=leadership,dc=example,dc=org'
echo $dn->getParent()->getParent(); // 'ou=leadership,dc=example,dc=org'
```

### Manipulating DNs

[](#manipulating-dns)

`Dn`, `Rdn` and `Attribute` are immutable so all manipulation functions return a new object.

#### Remove a fragment of a DN

[](#remove-a-fragment-of-a-dn)

Sometimes you may want to remove a fragment of a DN, for example its base DN.

```
$dn = LdapDn\Dn::fromString('cn=john.doe,ou=it,ou=leadership,dc=example,dc=org');
$fragmentToRemove = LdapDn\Dn::fromString('dc=example,dc=org');

echo $dn->withRemoved($fragmentToRemove); // 'cn=john.doe,ou=it,ou=leadership'
```

### Constructing DNs

[](#constructing-dns)

While the main purpose of the library is parsing DNs, you can also construct them.

```
use LdapDn\Dn;
use LdapDn\Rdn;
use LdapDn\Attribute;

$dn = new Dn([
    new Rdn([new Attribute('dc', 'org')]),
    new Rdn([new Attribute('dc', 'example')]),
    new Rdn([new Attribute('ou', 'leadership')]),
    new Rdn([new Attribute('ou', 'it')]),
    new Rdn([new Attribute('cn', 'doe, john'), new Attribute('uid', '123')]),
]);

echo $dn; // 'cn=doe\, john+uid=123,ou=it,ou=leadership,dc=example,dc=org'
```

Most RDNs contain a single attribute, so you can construct them with a shorthand syntax.

```
use LdapDn\Dn;
use LdapDn\Rdn;
use LdapDn\Attribute;

$dn = new Dn([
    Rdn::withNameAndValue('dc', 'org'),
    Rdn::withNameAndValue('dc', 'example'),
    Rdn::withNameAndValue('ou', 'leadership'),
    Rdn::withNameAndValue('ou', 'it'),
    new Rdn([new Attribute('cn', 'doe, john'), new Attribute('uid', '123')]),
]);

echo $dn; // 'cn=doe\, john+uid=123,ou=it,ou=leadership,dc=example,dc=org'
```

Exceptions
----------

[](#exceptions)

### AttributeNotFoundException

[](#attributenotfoundexception)

Thrown if an attribute is not found in an `Dn`.

### DnNotFoundException

[](#dnnotfoundexception)

Thrown if an `Dn` cannot be found in another `Dn`

### InvalidAttributeStringException

[](#invalidattributestringexception)

Thrown if a string representing an attribute is malformed.

### MultipleAttributesReturned exception

[](#multipleattributesreturned-exception)

Thrown when multiple `Attribute`s are returned when exactly 1 was expected.

### NotImplementedException

[](#notimplementedexception)

Thrown when an unimplemented method is called.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1879d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/657678?v=4)[Pawel Decowski](/maintainers/PawelDecowski)[@PawelDecowski](https://github.com/PawelDecowski)

---

Top Contributors

[![PawelDecowski](https://avatars.githubusercontent.com/u/657678?v=4)](https://github.com/PawelDecowski "PawelDecowski (23 commits)")

---

Tags

parserldapdnrdndistinguished name

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paweldecowski-ldap-dn/health.svg)

```
[![Health](https://phpackages.com/badges/paweldecowski-ldap-dn/health.svg)](https://phpackages.com/packages/paweldecowski-ldap-dn)
```

###  Alternatives

[nikic/php-parser

A PHP parser written in PHP

17.4k902.6M1.8k](/packages/nikic-php-parser)[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[erusev/parsedown

Parser for Markdown.

15.0k151.8M727](/packages/erusev-parsedown)[league/commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and GitHub-Flavored Markdown (GFM)

2.9k404.0M700](/packages/league-commonmark)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
