PHP fgetcsv - Skip columns? - php

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;
}

Related

I am trying to make a search bar in php to search search my csv file. However, I am not able to retrieve anything

I am trying to retrieve a row from my csv file.
For example:
//my csv file
Serial No., Name, Type, Category
12345, John, SG, Old
23456, Mary, MY, New
I am trying searching by serial number. What I want to achieve is something like this.
search: 12345
results: 12345, John, SG, Old
I tried using the method below
<?php
//from the previous page of my search bar
$sn = $_POST["sn"];
$result = [];
if (($handle = fopen("gameDirectory.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($data[1] == $sn)
print_r($data);
}
fclose($handle);
}
?>
It is not showing anything after i click search.
An array always start at 0.
Change $data[1] to $data[0].
Better code will nomalize both variable, somthing like :
<?php
//from the previous page of my search bar
$sn = intval($_POST["sn"]);
$result = [];
if (($handle = fopen("gameDirectory.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if(intval($data[0]) == $sn)
print_r($data);
}
fclose($handle);
}

How to read data from a web url and echo only a part (unique) to the page?

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);
}

Removing line from CSV if a date has passed

I'm opening a two-column CSV with states in one column and a delivery date in m/d/y format in the other. I'd like to remove duplicate state entries that have a date older than the most recent corresponding date.
For example, if the CSV contains:
CA,1/5/16
CA,4/8/15
CA,3/2/15
OR,3/4/15
OK,3/4/14
... I'd only want to store:
CA,1/5/16
OR,3/4/15
OK,3/4/14
Then, I want to compare the delivery dates in this new array to the current date and only show the states that have dates in the past (only OR and OK). To provide context, a delivery to a state comes in different parts, and I only want to list states with fully-delivered shipments.
I'm stuck on the comparing and removing dates part, but think I have the rest. So far, I have:
<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
$row=0;
$csv_delivered = array();
while (($line_of_text = fgetcsv($handle, 1000, ",")) !== FALSE) {
$date=$line_of_text[1];
if(\DateTime::createFromFormat('m/d/y',$date) < new \DateTime()){
//date is in the past
$csv_delivered[] = $line_of_text;
}
}
fclose($handle);
foreach ($csv_delivered as $delivered) {
$states_delivered.= $delivered[0].","; //state
}
}
?>
<p>Delivered: <?php echo $states_delivered; ?></p>
If I got you right,
<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
$row=0;
$csv_delivered = array();
$state_date = array(); // holds date of last occurence
$now = new \DateTime();
while (($line_of_text = fgetcsv($handle, 1000, ",")) !== FALSE) {
// ignore data that are in the future
if ($state_date_min[$state]>$now) continue;
// prepare
$date=$line_of_text[1];
$state=$line_of_text[0];
$date = \DateTime::createFromFormat('m/d/y',$date);
// store if not existing or overwrite if this one is newer
if (!isset($state_date[$state]) || $date>$state_date[$state]) {
$csv_delivered[$state] = $line_of_text;
$state_date[$state] = $date;
}
}
fclose($handle);
foreach ($csv_delivered as $state => $delivered) {
$states_delivered.= $delivered[0].","; //state
}
}
?>
<p>Delivered: <?php echo $states_delivered; ?></p>
Remember the date for each state and store if not existing or overwrite if newer. Do ignore all data that are in the future.

Using PHP to search CSV by Column?

Having trouble searching CSV file by Column. For example, I have the following CSV file:
NAME, HAS AN IPHONE, HAS ANDROID
bob, yes, no,
fred, no, yes,
How could I search column 2 for a 'yes' value using php,
then return only the rows with "yes" in second column results?
I think, This can help you. Read about fgetcsv
<?php
$result = [];
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($data[1] == 'yes') //Checks second column is 'yes'
array_push($result, $data);
}
fclose($handle);
var_dump($result);
}
?>
Assuming you have already parsed the csv file into an array.
The second parameter of array_keys function is mixed $search_value
If specified, then only keys containing these values are returned.
To search a specific column, use array_column:
$res = array_keys(array_column($csv, 1), "yes");
See test at eval.in; This would return keys of of all "yes" matches in the second column.
Have you tried anything if so please post that also, you may find this helpful
$csv = array_map('str_getcsv', file('data.csv'));
foreach($csv as $line){
if($line[1] == 'yes'){
//do what ever you want
echo "found yes";
}
}
Well this is what i tried and it worked for searching for a value in rows.
Then you can get the values of the rows and use it.
<?php
$file = fopen("test.csv", "r");
$str = "n#gmail.com";
while (($data = fgetcsv($file)) !== false)
{
if(in_array($str, $data))
{
foreach ($data as $i)
{
echo $i."<br>";
}
}
}
fclose($file);
?>

PHP use the fputcsv CSV to write same data in column for each row

I am trying to put a new data "exemple" for each line of the CSV. The lines of the csv varies. The header column name would be ExempleRow
There is not separator only a delimiter which is the semi colon.
I'm using fputcsv, it must have an array to fill the csv file with desired data. But in my case the number of lines changes for each csv.
So far it adds the new column with but I can't understand how to put the same value in each line for that column ?
<?php
$newCsvData = array(); // here should I specify the same data value "exemple data" for each line ? but how ?
if (($handle = fopen("exemple.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 9999, ";")) !== FALSE) {
$data[] = 'ExempleRow';
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('exemple.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line,';',' ');
}
fclose($handle);
?>
If you want to show the keys of the array as the first column then you can do this. If you dont have an associative array or you want to hard code the column headers for what ever reason you can simply change the array_keys($line) for a hard coded array.
$handle = fopen('exemple.csv', 'w');
$keys = false;
foreach ($newCsvData as $line) {
if ($keys === false) {
//$header = array('HeaderOne', 'HeaderTwo', 'HeaderThree', 'HeaderFour'); Use this if you want to hard code your headers
fputcsv($handle, array_keys($line), ';', '');
$keys = true;
}
fputcsv($handle, $line,';',' ');
}
fclose($handle);

Categories