dmccabe Posted March 17, 2008 Share Posted March 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/ Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 this data doesn't happen to be the get data is it? try print_r($_GET); Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494047 Share on other sites More sharing options...
dmccabe Posted March 17, 2008 Author Share Posted March 17, 2008 No at the moment this is the data as pulled from LDAP. Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494052 Share on other sites More sharing options...
cooldude832 Posted March 17, 2008 Share Posted March 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494054 Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 What do you want to do with the data?? Are you looking to search through the string for a certain dept?? Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494055 Share on other sites More sharing options...
dmccabe Posted March 17, 2008 Author Share Posted March 17, 2008 Basically once they log in I want to catch what department they are a part of, then later on I can use this for what menu's they should see. Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494064 Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494077 Share on other sites More sharing options...
lordfrikk Posted March 17, 2008 Share Posted March 17, 2008 What about this? <?php $str = 'CN=IT Dept,OU=IT,OU=Head Office,OU=xxx.com,OU=xxxusers,DC=xxx,DC=com'; $v = preg_match('/^CN=(.+?),/', $str, $match); $dept = $match[1]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494085 Share on other sites More sharing options...
craygo Posted March 17, 2008 Share Posted March 17, 2008 Hate the guys that know Regex!!!! Ray Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494097 Share on other sites More sharing options...
dmccabe Posted March 17, 2008 Author Share Posted March 17, 2008 lol Tried yours first Ray and it worked perfectly, however i will use the RegEx if thats ok Quote Link to comment https://forums.phpfreaks.com/topic/96542-filtering-a-string-of-text-to-find-a-value/#findComment-494116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.