I'm trying to print out all the users in active directory using system("dsquery user"); in php, my problem is getting it trimmed down so I have an array containing all the users and nothing else, atm this is my code:
<?php
$test = system("dsquery user");
$teste = explode('CN=', $test);
print_r($teste);
$user = trim($teste[1], ",");
echo "<br \>" . $user;
?>
I can only fetch one user atm because the explode deletes everything else..
Any help is appreciated, basically what I wan't to have in the end is something like this:
$user[0] = Administrator
$user[1] = kbgrt
$user[2] = asdasd
This is the 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 you understand otherwise comment and I'll try to explain in another way.
Its not always easy to parse output from commands, if they are not designad for it. However a good start is to try to simplify it as much as possible and see patterns. However hope the code bellow will work for you I try to commented it as much as possible.
I have started with your output from 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 make it easier I remove spaces and " from the string
$test = str_replace(' ', ',', $test);
$test = str_replace('"', '', $test);
Now we are ready to split the string on , so we get an array $teste that has elements that starts with LDIF values("DC=" or "CN=" in our case)
$teste = explode(',', $test);
Since some users are unwanted I add an array to exclude them. Also create an array $users to keep the result.
$users = array();
$exclude_users = array("Users", "doctor.scripto");
Then its just to iterate over array and split each element, so we get the LDIF part in position 0 and name in position 1. Add the name, if position 0 is equal to CN and name in position 1 is not in the list of excluded users.
foreach ($teste as $value) {
$data = explode('=', $value);
if($data[0] == 'CN' && !in_array($data[1], $exclude_users)) {
$users[] = $data[1];
}
}
print_r($users);
Here is the result:
Array
(
[0] => Administrator
[1] => Guest
[2] => krbtgt
)
Related
I am trying to get dir name from my file path: Currently below is my output:
Array
(
[path] => \documentLibrary\work\marketingProcess\BusinessMarketing\Design\Images\02_Product_Images\04_SHORT-RANGE\NINA\01_NINA-B1\source_WEB
)
and I want to get second last name which is (01_NINA-B1). What I am trying is:
echo "<pre>".print_r(dirname($results),true)."</pre>"; die;
when I add dirname above it displays nothing. What can I try next?
$query = db_select('network_drive','networkd');
$query
->fields('networkd', array('path'))
->condition('ndid',$networkdriveid,'=')
->orderBy('networkd.ndid');
$results = $query->execute();
echo "<pre>".print_r(dirname($results['path']),true)."</pre>"; die;
This looks like a plain string operation:
split the string
get the desired element
Maybe like this:
$s = '/documentLibrary/work/marketingProcess/BusinessMarketing/Design/Images/02_Product_Images/04_SHORT-RANGE/NINA/01_NINA-B1/source_WEB';
$a = explode(DIRECTORY_SEPARATOR, dirname($s)); //removes source_WEB and splits the string
echo array_pop($a); //gets the last element '01_NINA-B1'
Demo
Is this drupal? from my research I found out that db_select is a drupal function.
Can you try this one? it will attempt to explode the path string and find the second last element, if none is found it will return "N/A"
$query = db_select('network_drive','networkd');
$query
->fields('networkd', array('path'))
->condition('ndid',$networkdriveid,'=')
->orderBy('networkd.ndid');
$results = $query->execute();
foreach($results as $result){
$array = explode("\\", $result->path);
echo isset($array[count($array) - 2]) ? $array[count($array) - 2] : "N/A";
}
I am trying to group some foreach results based on the first 3 sets of characters.
For example i am currently listing sku codes for products and they look like this:
REF-MUSBOM-0500-ORA
REF-PROCOF-0001-LAT
REF-WHEREF-0001-TRO
REF-WHEREF-0001-ORA
REF-SHAKER-0700-C/B
REF-CREMON-0100-N/A
REF-GLUSUL-0090-N/A
REF-CRECAP-0090-N/A
REF-ALBFER-0120-N/A
REF-TSHCOT-LARG-BLK
REF-TSHCOT-MEDI-BLK
REF-ALBMAG-0090-N/A
REF-GYMJUG-2200-N/A
REF-OMEGA3-0090-N/A
REF-NEXGEN-0060-N/A
REF-VITAD3-0100-N/A
REF-SSSHAK-0739-N/A
REF-GINKGO-0090-N/A
REF-DIGEZY-0090-N/A
REF-VEST00-MEDI-N/A
REF-VEST00-LARG-N/A
REF-CREMON-0250-N/A
REF-MSM----0250-N/A
REF-GRNTEA-0100-N/A
REF-COLOST-0100-N/A
REF-GLUCHO-0090-N/A
REF-ZINCMA-0100-N/A
REF-BETALA-0250-N/A
REF-DRIBOS-0250-N/A
REF-HMB000-0090-N/A
REF-ALACID-0090-N/A
REF-CLA000-0090-N/A
REF-ACETYL-0090-N/A
REF-NXGPRO-0090-N/A
REF-LGLUTA-0250-N/A
REF-BCAA20-0200-N/A
REF-FLAPJA-0012-ACR
REF-FLAPJA-0012-MAP
REF-LCARNI-0100-N/A
REF-CORDYC-0090-N/A
REF-CREMON-0500-N/A
REF-BCAAEN-0330-APP
REF-PREWKT-0300-FPU
REF-TESFUS-0090-N/A
REF-AMIIFUS-0300-GAP
REF-AMIIFUS-0300-WME
REF-BCAINT-0400-FPU
REF-KRILLO-0090-N/A
REF-AMIIFUS-0300-PLE
REF-AMIIFUS-0300-FPU
REF-BCAINT-0400-WME
REF-ENZQ10-0090-N/A
REF-THERMO-0100-N/A
REF-LGLUTA-0500-N/A
REF-RBAR00-0012-DCB
REF-RBAR00-0012-PBC
REF-RBAR00-0012-WCR
REF-IMHEAV-2200-CHO
REF-PROCOF-0012-N/A
REF-DIEPRO-0900-STR
REF-DIEPRO-0900-BOF
REF-DIEPRO-0900-CHO
REF-INWPRO-0900-VAN
REF-INWPRO-0900-BOF
REF-INWPRO-0900-BCS
REF-INWPRO-0900-CHO
REF-INWPRO-0900-CMI
REF-INWPRO-0900-RAS
REF-INWPRO-0900-STR
REF-INWPRO-0900-CIN
REF-INWPRO-0900-CPB
REF-EGGPRO-0900-CHO
REF-EGGPRO-0900-VAN
REF-MICCAS-0909-CHO
REF-MICCAS-0909-CMI
REF-MICCAS-0909-VAN
REF-MICCAS-0909-STR
REF-BCAA50-0500-N/A
REF-MICWHE-0909-STR
REF-MICWHE-0909-VAN
REF-MICWHE-0909-CHIO
REF-MICWHE-0909-BAN
REF-1STOXT-2030-STR
REF-1STOXT-2030-VAN
REF-1STOXT-2030-CHO
REF-MUSBOM-0600-BCH
REF-MUSBOM-0600-FPU
REF-MUSBCF-0600-BCH
REF-MUSBCF-0600-FPU
REF-VEGANP-2100-STR
REF-VEGANP-2100-CHO
REF-INMPRO-2270-CPB
REF-DIETMR-2400-CPB
REF-INMPRO-2270-SCR
REF-INMPRO-2270-VIC
REF-MATRIX-1800-FRU
REF-INMPRO-2270-BOF
REF-MATRIX-1800-CHO
REF-INMPRO-2270-CHO
REF-ONESTO-2100-CHO
In the above list there are 2 skus which are:
REF-WHEREF-0001-TRO
REF-WHEREF-0001-ORA
The first 3 sets of characters split by - are the same. What would be the best approach of grouping all results leaving me an array something like this:
Array
(
[REF-WHEREF-0001] => Array
(
[0] => REF-WHEREF-0001-TRO
[1] => REF-WHEREF-0001-ORA
)
)
Are the first 3 groups (excluding the multiple -) always 13 characters? Then do something like this:
<?php
$arr = ["REF-MUSBOM-0500-ORA",
"REF-PROCOF-0001-LAT",
"REF-WHEREF-0001-TRO",
"REF-WHEREF-0001-PPL"];
$resultArr = [];
foreach ($arr as $sku) {
$resultArr[substr($sku, 0, 15)][] = $sku;
}
var_dump($resultArr);
If that length varies you might want to work with a regex or the strpos() of the third -.
I must say that I think you could come up with this yourself, since you were already thinking in the right direction i.e. foreach()
EDIT: Because I found other solutions more elegant looking, I decided to compare efficiency. This solution is a lot faster than the other ones.
I always create a new array with the index that I need for group, try this:
$arr=array('REF-MUSBOM-0500-ORA',
'REF-PROCOF-0001-LAT',
'REF-WHEREF-0001-TRO');
$newarr=array();
foreach($arr as $a){
$b=explode('-',$a);
array_pop($b);
$b=implode("-", $b);
$newarr[$b][]=$a;
}
echo '<pre>',print_r($newarr),'</pre>';
You will need to pick a group with some basics use of explode, implode and str_replace.
What does this solution do.
loop through the array of your items
explode to get last item index of exploded string assuming that it
would be dynamic in the end
implode & str_replace again to find out string of group name
And last strpos & in_array to have sample reponse
Solution
$array = array(
'REF-MUSBOM-0500-ORA',
'REF-PROCOF-0001-LAT',
'REF-WHEREF-0001-TRO',
'REF-WHEREF-0001-ORA',
'REF-SHAKER-0700-C/B',
'REF-CREMON-0100-N/A',
'REF-GLUSUL-0090-N/A',
'REF-CRECAP-0090-N/A',
'REF-ALBFER-0120-N/A',
);
$new_array = array();
foreach ($array as $key => $val) {
$group_arr = explode('-', $val);
$end = end($group_arr);
$combined_group = implode('-', $group_arr);
$group = str_replace('-' . $end, '', $combined_group);
if (strpos($val, $group) !== false && !in_array($group, $new_array)) {
$new_array[$group][] = $val;
}
}
echo '<pre>';print_r($new_array);echo '</pre>';
See demo on Sandbox
I've been searching and searching and can't find anything that works, but this is what I want to do.
This code:
try{
$timeout = 2;
$scraper = new udptscraper($timeout);
$ret = $scraper->scrape('udp://tracker.openbittorrent.com:80',array('0D7EA7F06E07F56780D733F18F46DDBB826DCB65'));
print_r($ret);
}catch(ScraperException $e){
echo('Error: ' . $e->getMessage() . "<br />\n");
echo('Connection error: ' . ($e->isConnectionError() ? 'yes' : 'no') . "<br />\n");
}
Outputs this:
Array ( [0D7EA7F06E07F56780D733F18F46DDBB826DCB65] => Array ( [seeders] => 148 [completed] => 10 [leechers] => 20 [infohash] => 0D7EA7F06E07F56780D733F18F46DDBB826DCB65 ) )
And I want that seeder count into a string such as $seeds. How would I go about doing this?
Something like this?
$seeds = $ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders'];
you can user strval() to convert a number to a string.
$string = strval($number);
or you can cast it into a string:
$string = (string)$number;
in your context that would be:
$string = strval($ret['0D7EA7F06E07F56780D733F18F46DDBB826DCB65']['seeders']);
However that odd string is also the first index of the array so you could also do it like this:
$string = strval($ret[0]['seeders']);
or if you want ot use only indexes ('seeders' is also the first index of the second array):
$string = strval($ret[0][0]);
if you just want the number then it's easy too:
$num = $ret[0][0];
It's not clear if you're looking to assign the array value(s?) as a separate variable(s?) or just to cast it into a string. Here's a nice way to accomplish all the above options, by assigning each array key as a separate variable with the matching array value:
$ret_vars = array_pop($ret);
foreach ($ret_vars as $variable_name=>$variable_value) :
${$variable_name} = (string)$variable_value;
endforeach;
In your original example, this would end up populating $seeders, $completed, $leechers and $infohash with their matching string values. Of course, make sure these variable names are not used/needed elsewhere in the code. If that's the case, simply add some sort of unique prefix to the ${} construct, like ${'ret_'.$variable_name}
Edit: The aim of my method is to delete a value from a string in a database.
I cant seem to find the answer for this one anywhere. Can you concatenate inside a str_replace like this:
str_replace($pid . ",","",$boom);
$pid is a page id, eg 40
$boom is an exploded array
If i have a string: 40,56,12 i want to make it 56,12 however without the concatenator in it will produce:
,56,12
When I have the concat in the str_replace it doesnt do a thing. Is this possible?
Answering your question: yes you can. That code works as you would expect it to.
But this approach is wrong. It will not work for $pid = 12; (last element, without trailing coma) and will incorrectly replace 40, in $boom = '140,20,12';
You should keep it in array, search for unwanted value, if found unset it from the array and then implode with coma.
$boom = array_filter($boom);
$key = array_search($pid, $boom);
if($key !== false){
unset($boom[$key]);
}
$boom = implode(',',$boom);
[+] Your code does not work because $boom is an array, and str_replace operates on string.
As $boom is an array, you don't need to use array on your case.
Change this
$boom = explode(",",$ticket_array);
$boom = str_replace($pid . ",","",$boom);
$together = implode(",",$boom);
to
$together = str_replace($pid . ",","",$ticket_array);
Update: If you want still want to use array
$boom = explode(",",$ticket_array);
unset($boom[array_search($pid, $boom)]);
$together = implode(",",$boom);
After you have edited it becomes clear that you want to remove the value of $pid from the array $boom which contains one number as a value. You can use array_search to find if it is in at if in with which key. You can then unset the element from $boom:
$pid = '40';
$boom = explode(',', '40,56,12');
$r = array_search($pid, $boom, FALSE);
if ($r !== FALSE) {
unset($boom[$r]);
}
Old question:
Can you concatenate inside a str_replace like this: ... ?
Yes you can, see the example:
$pid = '40';
$boom = array('40,56,12');
print_r(str_replace($pid . ",", "", $boom));
Result:
Array
(
[0] => 56,12
)
Which is pretty much like you did so you might be looking for the problem at the wrong place. You can use any string expression for the parameter.
It might be easier for you if you're unsure to create a variable first:
$pid = '40';
$boom = array('40,56,12');
$search = sprintf("%d,", $pid);
print_r(str_replace($search, "", $boom));
You should store your "ticket array" in a separate table.
And use regular SQL queries (UPDATE, DELETE) to manipulate it.
A relational word in the name of your database is for the reason. And you are abusing this smart software with such a barbaric approach.
You could use str_split, it converts a string to an array, then with a foreach loop echo all the values except the first one.
$numbers_string="40,56,12";
$numbers_array = str_split($numbers_string);
//then, when you have the array of numbers, you could echo every number except the first separating them with a comma
foreach ($numbers_array as $key => $value) {
if ($key > 0) {
echo $value . ", ";
}
}
If you want is to skip a value not by it's position in the array, but for it's value then you could do this instead:
$unwanted_value="40";
foreach ($numbers_array as $key => $value) {
if ($value != $unwanted_value) {
echo $value . ", ";
}
else {
unset($numbers_array[$key]);
$numbers_array = array_values($numbers_array);
var_dump($numbers_array);
}
}
I am stuck working on a problem, and would appreciate some guidance. I am working with an old system that was coded awfully, and didnt do any validating, or sanitzing of user input, so the database I'm working with is a bit missy.
I have issues with one column called "tags", used for tags for articles. But each row appears like this, and varies between them:
tag tag1 tag2 tag3
OR
tag1, tag2, tag
But I need to combine them and put them into an array that lists them by how many times each one occurs, like this:
tag(2)
tag1(2)
tag2(2)
tag3(1)
Because I'm not the most astute with php, Im not sure if I am going about the problem correctly?
Im new to manipulating arrays on this scale which is why I am stuck at this point. You can view the example below of what I am doing below:
$sql = "SELECT tags, approved FROM articles WHERE approved = '1' ";
$result = mysql_query($sql);
while( $rows = mysql_fetch_array($result) ) {
$arr = $rows['tags'];
$arr = str_replace(", ", " ", $arr); // attempting to clean up the data, so that each word appends with a space.
$arr = str_replace(" ", " ", $arr);
// Don't know what to do next, or if this is even the right way to do it.
}
Any help is appreciated.
Thanks, Lea
Maybe this will be helpful. Modified your code to have a $tagArr for each query. If you need an overall array of tags it would be a bit different, but could easily be coded using the following.
while( $rows = mysql_fetch_array($result) ) {
$arr = $rows['tags'];
$arr = str_replace(", ", " ", $arr); // attempting to clean up the data, so that each word appends with a space.
// Don't know what to do next, or if this is even the right way to do it.
$tagArr = array();
$current_tags = explode(" ", $arr);
foreach($current_tags as $tag) {
if(!array_key_exists($tag, $tagArr)) {
$tagArr[$tag] = 1;
} else {
$tagArr[$tag] = $tagArr[$tag]++;
}
}
// now $tagArr has the tag name as it's key, and the number of occurrences as it's value
foreach($tagArr as $tag => $occurrences) {
echo $tag . '(' . $occurrences . ')<br />';
}
}