SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Return everythuing after '=' and before ',' ?

    I have a variable that represents a user string (it's an L-DAP string). From this variable, I need to return everything before the first comma and everything after the first equals sign.

    Here's an example of the string value:
    CN=mrfoobar11,OU=XYZ,OU=ABC Users,DC=blahad,DC=foobar,DC=com
    Essentially, I just need "mrfoobar11" and that's all. I tried using substr to get everything after the first equals sign but when I tried to do the same thing with everything up to the first comma, I began to think I needed a RegEx--which I'm betting I don't.

    Any suggestions / hints is appreciated.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,244
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    Well you definitely can do it using substr (or regex)
    PHP Code:
    $string 'CN=mrfoobar11,OU=XYZ,OU=ABC Users,DC=blahad,DC=foobar,DC=com';
    $firstEqualSign strpos($string'=') + 1;
    $firstComma strpos($string','$firstEqualSign);
    $phraseToKeep substr($string$firstEqualSign$firstComma $firstEqualSign);
    echo 
    $phraseToKeep
    Then for regex
    PHP Code:
    $matches = array();
    $phraseToKeep '';
    $string 'CN=mrfoobar11,OU=XYZ,OU=ABC Users,DC=blahad,DC=foobar,DC=com';
    if (
    preg_match('/CN=([a-z0-9]+),/'$string$matches))
    {
      
    $phraseToKeep $matches[1];
    }
    echo 
    $phraseToKeep

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Here's one that will allow you to use all the vars. Then you can go right to the first if you'd like. I feel like theres something out there similar to urldecode() that should do this.
    Code PHP:
    $string = "CN=mrfoobar11,OU=XYZ,OU=ABC Users,DC=blahad,DC=foobar,DC=com";
    $arr = explode(",", $string);
    $final = array();
    foreach($arr as $var) {
        $var2 = explode("=", $var);
        array_push($final, array("var" => $var2[0], "val" => $var2[1]));
    }
    print_r($final);

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    I can't test on my machine as I don't have the function, but have a look at this:

    http://php.net/manual/en/function.ldap-explode-dn.php

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    or you could str_replace the ,'s into &'s and parse it as a query string...

    EDIT: No you cant, because there is more than one DC. Hurr.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone. I went ahead and used cpradio's strpos approach. Got me what I needed.

    Kyle, I never thought about possibly using all the variables... I'll have to keep that in mind because that might come into play here. And I didn't even realize that PHP came with some libraries / functions that did stuff with ldap. That's pretty cool. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •