I have a string which is CN=van der Valk\, Marco,OU=UT,OU=NL,OU=EMEA,OU=associates,OU=usersAndGroups,DC=corporate,DC=ingrammicro,DC=com
.
I only want what is after the CN
which will be van der Valk
in this case.
I tried it with the trim()
function, but didn't succeed. Can anybody help me?
You can use strpos.
First I find the position of CN=
and then use that as the offset in the second strpos (to find the end of string).
$str = "CN=van der Valk\, Marco,OU=UT,OU=NL,OU=EMEA,OU=associates,OU=usersAndGroups,DC=corporate,DC=ingrammicro,DC=com";
$CN = strpos($str, "CN=")+3; // +3 because CN= is three characters.
Echo substr($str, $CN, strpos($str, '\\', $CN)-3); // -3 because we need to subtract what we added above.
$str = 'CN=van der Valk\, Marco,OU=UT,OU=NL,OU=EMEA,OU=associates,OU=usersAndGroups,DC=corporate,DC=ingrammicro,DC=com';
preg_match("/CN=([\w\s\\\\,]+),/", $str, $match);
List($lastname, $firstname) = explode(',', str_replace("\\", "", $match[1]));
Echo $firstname ." " . $lastname;