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.

Well you definitely can do it using substr (or regex)

$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

$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;

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.

$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]));

I can’t test on my machine as I don’t have the function, but have a look at this:

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.

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.