tags:

views:

39

answers:

1

Hi all,

I need to extract information from an LDAP connection string like this one:

ldap://uid=adminuser,dc=example,c=com:[email protected]/dc=basePath,dc=example,c=com

I want to use a regular expression that will extract each token for me and place it in an array. I tried some regex expression and the best I got was this:

/(\w+)\:\/\/(([a-zA-Z0-9]+)(:([^\:\@]+))?@)?([^\/]+)((\/[^#]*)?(#(.*))?)/

But unfortunately, when I execute it in PHP, I get the following result:

Array
(
    [0] => ldap://uid=adminuser,dc=example,c=com:[email protected]/dc=basePath,dc=example,c=com
    [1] => ldap
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => uid=adminuser,dc=example,c=com:[email protected]
    [7] => /dc=basePath,dc=example,c=com
    [8] => /dc=basePath,dc=example,c=com
)

Can anybody help me so that I can correctly extract each part of the connection string?

Thanks

+5  A: 

([a-zA-Z0-9]+) seems quite inadequate to pick up an LDAP string containing = and other characters.

In general you should not try to parse URLs yourself with regex. Instead, use the standard library function parse_url().

This gives:

array(5) {
  ["scheme"]=>
  string(4) "ldap"
  ["host"]=>
  string(16) "ldap.example.com"
  ["user"]=>
  string(30) "uid=adminuser,dc=example,c=com"
  ["pass"]=>
  string(6) "secret"
  ["path"]=>
  string(29) "/dc=basePath,dc=example,c=com"
}
bobince
WOW! That is way better than a regex:) Thanks a lot!
MaxP