Jump to content

Filtering a string of text to find a value


dmccabe

Recommended Posts

Ok lets say I have a string like this:

 

CN=IT Dept,OU=IT,OU=Head Office,OU=xxx.com,OU=xxxusers,DC=xxx,DC=com

 

Now the layout of the string will be the same always and the section I want to capture iswhere it says "IT Dept".  So after the first = and before the first ,.

 

However the value itself will vary depending on what department the user is part of.

well assuming you left the comma as a reserved character for delimination you can explode it then

 

<?php
$string = "CN=IT Dept,OU=IT,OU=Head Office,OU=xxx.com,OU=xxxusers,DC=xxx,DC=com";
$string= explode(",",$string);
Print_r($string);
?>

 

Only issue with this is the keys are lost so you will need to find the keys in it

So the CN is the department they are in correct?? and it is always the first entry, correct.

 

try this if what I assume is correct

<?php
$string = "CN=IT Deptartment,OU=IT,OU=Head Office,OU=xxx.com,OU=xxxusers,DC=xxx,DC=com";
$start = strpos($string, "=");
$end = strpos($string, ",");
$pos = $end - $start - 1;
$dept = substr($string, 3, $pos);

echo $dept;
?>

 

You could always put it in a function.

<?php
$string = "CN=IT Deptartment,OU=IT,OU=Head Office,OU=xxx.com,OU=xxxusers,DC=xxx,DC=com";

function find_dept($string){
$start = strpos($string, "=");
$end = strpos($string, ",");
$pos = $end - $start - 1;
$dept = substr($string, 3, $pos);
return $dept;
}

$dept = find_dept($string);

echo $dept
?>

 

 

 

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.