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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.