Regular expression to match an ldap connection string
I need to extract information from an LDAP connection string like this one:
ldap://uid=adminuser,dc=example,c=com:secret@ldap.example.com/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:secret@ldap.example.com/dc=basePath,dc=example,c=com
[1] => ldap
[2] =>
[3] =>
[4] =>
[5] =>
[6] => uid=adminuser,dc=example,c=com:secret@ldap.example.com
[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
([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"
}
精彩评论