I have an array usergroup and I am echoing the values using for loop, the issue is there's one value that looks like this
'CN=WebHelpDesk,OU=Groups,OU=Something,DC=someCompany,DC=org'
an array to be exact,
'CN=WebHelpDesk1,OU=Groups,OU=Something,DC=someCompany,DC=org'
'CN=WebHelpDesk2,OU=Groups,OU=Something,DC=someCompany,DC=org'
'CN=WebHelpDesk3,OU=Groups,OU=Something,DC=someCompany,DC=org'
All I want from this string is the value of CN,
I succeeded removing the "CN=" using this line
$snip = substr($_SESSION['userGroup'][$i], "3");
the problem now is that how can I am remove the strings after the value for "CN=" since the values doesn't have a static length size.
Try this.
$data = "CN=WebHelpDesk,OU=Groups,OU=Something,DC=someCompany,DC=org";
$tmp = explode(",", $data);
$assoc = array();
foreach($tmp as $t){
$values = explode("=", $t);
$assoc[$values[0]] = $values[1];
}
// Now get wahtever value you from the $assoc array, i.e.
echo $assoc['CN'];
A regular expression would be the most versatile and easiest way to extract the information, though it does have a bit of a learning curve:
preg_match('/CN=(.*?),/', $string, $matches);
The CN should now be contained in the $matches array. You could also use a substring check:
$cn = substr($string, 2, strpos($string, ','));