I am creating this array from a posted textarea and splitting the lines:
$ignored = array();
foreach(explode("\n", $_POST["ignored"]) as $ignored2) {
$ignored[] = $ignored2;
echo $ignored2.'<br>';
}
then i have a while loop where i check if any of the array items are in a variable within the while loop:
while(($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//check if the description is anything in the ignore array
if(in_array($data[6], $ignored)) {
echo 'ignore';
} else {
echo 'dont ignore';
}
}
I put the following in the textarea (one on each line):
SIP Trunk: ST17830T001 (200 channels)
SIP Trunk: ST17830T002 (1 channels)
but its only echoing 'ignore' once and its not ignoring the other item (they both exist in the $data[6] variable
Trim the data from the textarea:
$ignored = array();
foreach(explode("\n", $_POST["ignored"]) as $ignored2) {
$ignored[] = trim($ignored2);
echo $ignored2.'<br>';
}
Also trim the variable you're testing:
if (in_array(trim($data[6]), $ignored)) {
Related
For a project that bans abusers(malware downloaders) of networks automatically.
We need to get a unique list of IP's so our software can automatically block it at the gate before it can do any kind of damage.
Situation is the following:
We have data at an URL like the following: (Example: https://urlhaus.abuse.ch/feeds/asn/14061/)
Webdata:
# Dateadded (UTC),URL,URL_status,Threat,Tags,Host,IPaddress,number,Country
"2019-08-01 05:05:02","http://185.240.25.99/sparc","offline","malware_download","gafgyt|exploit|elf","185.240.25.99","185.204.25.99","14258","NL"
"2019-08-01 05:04:03","http://185.240.25.99/sh4","offline","malware_download","gafgyt|elf","185.240.25.99","185.240.25.99","14258","NL"
"2019-08-01 05:03:04","http://185.240.25.99/i686","offline","malware_download","elf|gafgyt|exploit","185.240.25.99","185.240.25.99","14258","NL"
"2019-08-01 05:03:02","http://185.240.25.99/mips","offline","malware_download","gafgyt|elf","185.240.25.99","185.240.25.99","14258","NL"
"2019-08-01 05:02:03","http://185.240.25.99/i586","offline","malware_download","gafgyt|elf","185.240.25.99","185.240.25.99","14258","NL"
"2019-07-31 14:06:10","http://185.240.25.115/dll/driver_update_service.sh4","online","malware_download","mirai|elf","185.240.25.115","185.240.25.115","14258","NL"
"2019-07-31 14:06:08","http://185.240.25.115/dll/driver_update_service.m68k","online","malware_download","mirai|elf","185.240.25.115","185.240.25.115","14258","NL"
"2019-07-31 14:06:06","http://185.240.25.115/dll/driver_update_service.ppc","online","malware_download","elf","185.240.25.115","185.240.25.115","14258","NL"
What i want is to return unique lines for the IP part.
The page should echo only the following unique ips like so:
185.240.25.99
185.240.25.115
Your file is CSV file(comma separated). There needs to skip first 11 lines which can be considered as a header. So I have started scanning it from row number 12.
Try to check below:
$row = 1;
$result = [];
if (($handle = fopen("https://urlhaus.abuse.ch/feeds/asn/14061/", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if($row < 12){ $row++; continue; }
$row++;
if ( !empty($data[5]) && !empty($data[6]))
{
if (!empty($data[5]))
array_push($result, $data[5]);
if (!empty($data[6]))
array_push($result, $data[6]);
}
}
echo "<pre>";
$test = (array_unique(array_values($result)));
foreach ($test as $key => $val) {
echo $val."<br>";
fclose($handle);
}
I am trying to turn an input file in the form below into a series of objects that can be manipulated.
arabian_sea_area = {
1926 1927 1931 1932 1933 2029 2030
}
gulf_of_aden_sea_area = {
1925 2024 5285 5286
}
sdf
<?php
$all_areas = array();
if (($handle = fopen("area.txt", "r")) == False)
{
die("failed to open file\n");
}
while (($line = fgets($handle)) !== FALSE)
{
if (ctype_alpha($line[0]))
{
$line= explode(" ",$line);
// echo($line[0]."\n");
$area = $line[0];
$IDs = explode(" ", fgets($handle));
$IDs[0] = ltrim($IDs[0], ' '); // trying to remove tab from first ID
$all_areas[$area] = $IDs;
//array_push($all_areas, $temp);
}
}
//echo("a\n");
print_r($all_areas["arabian_sea_area"]);
//var_dump ($all_areas);
?>
The values print correctly in the commented out debug lines but fail to print anything for the var_dump at the end.
edit: I realize this was unclear, what I was trying to do was create a master "all_areas" array that linked to objects titled the first line (ie. arabian_sea_area etc.) and I could then get at the numerical Ids for each area algorithmically for a later script.
There are many issues with your code:
1-
if (ctype_alpha($line[0]))
{
$line= explode(" ",$line);
//echo($line[0]."\n");
$temp = $line[0];
$temp = new Area;
$temp->filler($line, $handle);
}
you are creating a $temp variable but you forgot to push it to your main array $all_areas. use array_push
2-
var_dump ($arabian_sea_area);
$arabian_sea_area does not exist.
Did you mean to print your main array $all_areas ?
3- Recommendation:
On errors (echo("failed to open file\n");) its recommended to use die("failed to open file\n"); instead of echo. as die will stop the rest of the script from executing.
-- UPDATE --
I edited your code in a way that should work fine:
class Area {
public $area_name;
public $IDs = array();
public function filler($line, $handle) {
$this->area_name = $line[0];
//echo($this->area_name."\n");
$this->IDs = explode(" ", fgets($handle));
//print_r($this->IDs);
}
}
$all_areas = array();
if (($handle = fopen("area.txt", "r")) == False)
{
die("failed to open file\n");
}
while (($line = fgets($handle)) !== FALSE)
{
if (ctype_alpha($line[0]))
{
$line= explode(" ",$line);
// echo($line[0]."\n");
$temp = $line[0];
$temp = new Area;
$temp->filler($line, $handle);
array_push($all_areas, $temp);
}
}
//echo("a\n");
var_dump ($all_areas);
You might wanna update it to remove / filter empty values.
At the moment I have a script that will remove the row in my csv when it has already seen the sku before.
Here is the script:
<?php
// array to hold all unique lines
$lines = array();
// array to hold all unique SKU codes
$skus = array();
// index of the `sku` column
$skuIndex = -1;
// open the "save-file"
if (($saveHandle = fopen("unique.csv", "w")) !== false) {
// open the csv file
if (($readHandle = fopen("original.csv", "r")) !== false) {
// read each line into an array
while (($data = fgetcsv($readHandle, 8192, ",")) !== false) {
if ($skuIndex == -1) {
// we need to determine what column the "sku" is; this will identify
// the "unique" rows
foreach ($data as $index => $column) {
if ($column == 'sku') {
$skuIndex = $index;
break;
}
}
if ($skuIndex == -1) {
echo "Couldn't determine the SKU-column.";
die();
}
// write this line to the file
fputcsv($saveHandle, $data);
}
// if the sku has been seen, skip it
if (isset($skus[$data[$skuIndex]])) continue;
$skus[$data[$skuIndex]] = true;
// write this line to the file
fputcsv($saveHandle, $data);
}
fclose($readHandle);
}
fclose($saveHandle);
}
?>
This works fine but I am starting to need the content that is deleted.
What i need now, is to modify the code to add the same prefix to all duplicate sku's as there will only be 2 of the same sku.
I do not know where to start.
Adding a prefix to duplicates
This will add a prefix to any duplicate SKU and will then store it into the unique CSV output, e.g. XYZ123 becomes duplicate-XYZ123.
Change:
if (isset($skus[$data[$skuIndex]])) continue;
to:
if (isset($skus[$data[$skuIndex]])) $data[$skuIndex] = 'duplicate-' . $data[$skuIndex];
Fixing the duplicate header row
Add continue; after fputcsv($saveHandle, $data); Inside if ($skuIndex == -1) {. Because fputcsv(...) appears twice in the loop, it will be run twice on the first iteration of the loop.
I have the csv file(test.csv) and have the text as below:
1,maly,maly(),f,df
2,cheata,aaa,df,df
3,cheata,df,df,df
4,maly,fc,cfv,f
5,maly,df,fg,fg
6,chantha,fc,gf,fg
7,chantha,gh,a,g
8,David,fgfd,dfg,g
What I want:
I want to diplay only:maly cheata chantha David.For the name that have two or more the same,take only one.And I have the php code as below:
$c=0;
$data=fopen('test.csv','r');
while($row=fgets($data)){
if($c!=0){
echo $row[3]."<br>\n";
}
$c++;
}
The problem is
It does not display what I want. It displays
h h a a h h a
How do I fix this?
Use fgetcsv instead of fgets.
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (isset($data[1])) {
echo $data[1] . "<br>\n";
}
}
fclose($handle);
}
It looks like you were intending to call fgetcsv instead of fgets. Also, the name index would be 1 then, instead of 3:
<?php
$file = fopen('test.csv', 'r');
while($row = fgetcsv($file)) {
if (isset($row[1])) {
echo $row[1], "\n";
}
}
fclose($file);
The file method returns all lines in an array. The explode method splits up a line by a particular delimiter.
$lines = file($file_name);
$fields = array();
foreach ($lines as $line) {
$cells = explode(',', $line);
$fields[] = $cells[1];
}
echo "<pre>";
print_r($fields);
echo "</pre>";
You're heading does not match your question, you want the second column, not row.
Firstly, your getting the 4th character of each row you need to do something like this (not tested):
$data=fopen('test.csv','r');
while($row=fgets($data)){
$cols = explode(',' $row);
echo $cols[1]."<br>\n";
}
I am trying to import a CSV, and be able to remove columns, and only import the ones selected.
So I have an array of selected fields, and the CSV file. I can remove rows, but having problems skipping columns.
Here is what i'm using to display a table, just trying to test out removing the column. Nothing fancy here. Just need to know where to place any if statements, or anything. Tips, suggestions, samples all welcome!
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
echo("\t<td>$val</td>\r\n");
}
echo("</tr>\r\n");
} //end while
Where do i place any if statement to allow only the remainder of rows? i've tried in_array, array_diff, etc - and nothing seems to be working?
Now that i'm typing this out, should I just use PHP to delete the columns before insert? What would be best practice?
thanks
jt
You can set an array containing the columns to display (in my ex 1 and 3) and use the key part of the foreach to know which is the column you are scanning:
<?
$handle = fopen("test.csv", "r");
$columns = array(1,3);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
if (in_array($index+1, $columns)) {
echo("\t<td>$val</td>\r\n");
}
}
echo("</tr>\r\n");
} //end while
?>
Try:
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
echo("<tr>\r\n");
foreach ($data as $index=>$val) {
if($index != BADINDEX) {
echo("\t<td>$val</td>\r\n");
}
}
echo("</tr>\r\n");
}
Where BADINDEX is the column number you want to get rid of (first column being 0).
Since what you get from fgetcsv are the rows and not the columns, one simply needs to filter out the desired column for every row printed, which is accomplished by checking the index in the foreach block.
This is old post but i found another way that may help someone:
$file = fopen($yourcsvfile, 'r');
while (($result = fgetcsv($file, 1000, ",")) !== false)
{
$temp = array();
$temp[] = $result[1]; // the column you want in your new array
$temp[] = $result[2]; // the column you want in your new array
$csv1[] = $temp;
}