Sergey Novikov
Sergey Novikov

Reputation: 4196

Strange encoding of ldap_explode_dn results

After converting DN string with ldap_explode_dn some characters (cyrillic in my case) are converted to other encoding that I can't recognize.

For example ldap_explode_dn('cn=tt,ou=groups,o=ц1,ou=realms,dc=uvf,dc=local', 0); return

Array
(
    [0] => tt
    [1] => groups
    [2] => \D1\861
    [3] => realms
    [4] => uvf
    [5] => local
)

As you can see, ц character was converted to \D1\86. I guess it is two-byte UTF-8, but I can't get how I can decode it.

I have tried many ways like iconv and mb_convert_encoding, but without success.

My question is - what is this encoding and how to work with it in PHP?

Upvotes: 0

Views: 492

Answers (1)

ahendwh2
ahendwh2

Reputation: 394

I found this user note on php.net which provides a solution for your problem.


EDIT: The modifier e is deprecated for preg_replace since PHP 5.5 so here is a solution with preg_replace_callback instead based on the linked user note:

function myldap_explode_dn( $dn, $with_attrib ) {
    $result = ldap_explode_dn( $dn, $with_attrib );

    //translate hex code into ascii again
    foreach ( $result as $key => $value ) {
        $result[ $key ] = preg_replace_callback(
            "/\\\([0-9A-Fa-f]{2})/",
            function ( $matches) {
                return chr( hexdec( $matches[1] ) );
            },
            $value
        );
    }

    return ( $result );
}

Upvotes: 1

Related Questions