php - 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:[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]+)(:([^\:\@]+))[email protected])?([^\/]+)((\/[^#]*)?(#(.*))?)/

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

    [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?





([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 .

This gives:

array(5) {
  string(4) "ldap"
  string(16) ""
  string(30) "uid=adminuser,dc=example,c=com"
  string(6) "secret"
  string(29) "/dc=basePath,dc=example,c=com"

People are also looking for solutions to the problem: image - I have this broken php upload script


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.