Reputation: 11
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?
Upvotes: 0
Views: 240
Reputation: 23958
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;
Upvotes: 2
Reputation: 1794
$str = 'CN=van der Valk\, Marco,OU=UT,OU=NL,OU=EMEA,OU=associates,OU=usersAndGroups,DC=corporate,DC=ingrammicro,DC=com';
// find position of 1st \, as \ is a string modifier we escape it with \\
$index = strpos($str, '\\');
echo "$index <br>";
// substr('string', 'starting index', 'length: numbers of character to get')
$new_string = substr($str, 3, $index - 3);
echo $new_string; // van der Valk
Upvotes: 0
Reputation: 7465
Here's an example
$val = "CN=van der Valk\, Marco,OU=UT,OU=NL,OU=EMEA,OU=associates,OU=usersAndGroups,DC=corporate,DC=ingrammicro,DC=com";
$valSplit = explode(",", $val)[0];
$firstVal = str_replace("\\", "", $valSplit);
print($firstVal);
OK, Explode splits the string into a series of arrays, based on the comma. str_replace takes 3 parameters. The first is what to replace, the second is what it should be replaced with, the third being the string that should be replaced. In this case, the goal is to remove the slash.
Upvotes: 0