php - Trim the content of system()? -
php - Trim the content of system()? -
i'm trying print out users in active directory using system("dsquery user"); in php, problem getting trimmed downwards have array containing users , nil else, atm code:
<?php $test = system("dsquery user"); $teste = explode('cn=', $test); print_r($teste); $user = trim($teste[1], ","); echo "<br \>" . $user; ?>
i can fetch 1 user atm because explode deletes else.. help appreciated, wan't have in end this:
$user[0] = administrator $user[1] = kbgrt $user[2] = asdasd
this output:
"cn=administrator,cn=users,dc=domain,dc=local" "cn=guest,cn=users,dc=domain,dc=local" "cn=krbtgt,cn=users,dc=domain,dc=local" "cn=doctor.scripto,cn=users,dc=domain,dc=local" –
i hope understand otherwise comment , i'll seek explain in way.
its not easy parse output commands, if not designad it. start seek simplify much possible , see patterns. hope code bellow work seek commented much possible.
i have started output system("dsquery user");
$test = '"cn=administrator,cn=users,dc=domain,dc=local" "cn=guest,cn=users,dc=domain,dc=local" "cn=krbtgt,cn=users,dc=domain,dc=local" "cn=doctor.scripto,cn=users,dc=domain,dc=local"';
to create easier remove spaces , " string
$test = str_replace(' ', ',', $test); $test = str_replace('"', '', $test);
now ready split string on , array $teste has elements starts ldif values("dc=" or "cn=" in our case)
$teste = explode(',', $test);
since users unwanted add together array exclude them. create array $users maintain result.
$users = array(); $exclude_users = array("users", "doctor.scripto");
then iterate on array , split each element, ldif part in position 0 , name in position 1. add together name, if position 0 equal cn , name in position 1 not in list of excluded users.
foreach ($teste $value) { $data = explode('=', $value); if($data[0] == 'cn' && !in_array($data[1], $exclude_users)) { $users[] = $data[1]; } } print_r($users);
here result:
array ( [0] => administrator [1] => invitee [2] => krbtgt ) php
Comments
Post a Comment