Search code examples
phpldapfilteringstring-search

filter word in string


I'm using PHP to query users and their attributes in Active Directory. The problem I have is that the LDAP field for Department Manager is not only returning the user name, but also the FQDN (fully qualified domain name) path as shown below:

CN=User Name,OU=Users,OU=companyBranchOffice,OU=companyName,DC=subdomain,DC=domain,DC=com

The result is saved in a string, $depManager. How can I filter out only the user name (CN=)?

Thanks in advance!


Solution

  • There is my simple (maybe not optimized) function to fetch informations from a DN :

    <?php
    function getInfosFromDN($dn)
    {
        $regexCaptures = "/^(CN=.+),(OU=.+),(DC=.+)$/iU";
    
        $CN = preg_replace($regexCaptures, "$1", $dn);
        $OU = preg_replace($regexCaptures, "$2", $dn);
        $DC = preg_replace($regexCaptures, "$3", $dn);
    
        return array($CN, $OU, $DC);
    }
    
    list($CN, $OU, $DC) = getInfosFromDN("CN=User Name,OU=Users,OU=companyBranchOffice,OU=companyName,DC=subdomain,DC=domain,DC=com");