Hack Forums
[PHP] How to parse csv file with php - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] How to parse csv file with php (/showthread.php?tid=5943443)



How to parse csv file with php - iStorePro - Feb 21, 2019

I have csv files but i want to parse and get the data in table or array and use them in others way
Like open links with that data


RE: How to parse csv file with php - Digital - Feb 21, 2019

PHP Code

<?php 
$file 
= 'sample.csv';
$file_handle = fopen($csvFile, 'r');
    while (!
feof($file_handle) ) {
        
$line_of_text[] = fgetcsv($file_handle, 1024);
    }
    
fclose($file_handle);
    echo 
json_encode($line_of_text); 



RE: How to parse csv file with php - Onireves - Feb 21, 2019

Learning to use Search Engines bro
https://stackoverflow.com/questions/9139202/how-to-parse-a-csv-file-using-php


RE: How to parse csv file with php - iStorePro - Feb 21, 2019

(Feb 21, 2019 04:20 AM)Digital Wrote:  
PHP Code

<?php 
 $file 
= 'sample.csv';
$file_handle = fopen($csvFile, 'r');
 
   while (!feof($file_handle) ) {
 
       $line_of_text[] = fgetcsv($file_handle, 1024);
 
   }
 
   fclose($file_handle);
 
   echo json_encode($line_of_text); 


Thanks but how to use that data to open them in browser as link for each one


(Feb 21, 2019 04:20 AM)Onireves Wrote:  Learning to use Search Engines bro

Nothing same what i want


RE: How to parse csv file with php - Digital - Feb 21, 2019

(Feb 21, 2019 04:24 AM)iStorePro Wrote:  Thanks but how to use that data to open them in browser as link for each one



Nothing same what i want

Change it into these:

PHP Code

<?php 
while (!feof($file_handle) ) {
        
$url = fgetcsv($file_handle, 1024);
echo 
"<script>window.open('".$url."')</script>";
    } 



RE: How to parse csv file with php - iStorePro - Feb 21, 2019

(Feb 21, 2019 04:35 AM)Digital Wrote:  Change it into these:

PHP Code

<?php 
while (!feof($file_handle) ) {
 
       $url = fgetcsv($file_handle, 1024);
echo 
"<script>window.open('".$url."')</script>";
 
   } 

Big thanks ?


RE: How to parse csv file with php - Abruptum - Feb 21, 2019

(Feb 21, 2019 05:06 AM)iStorePro Wrote:  Big thanks ?

The only valid answer here was from Onirevs ...

Wrote you a full Solution how it could look like:

PHP Code

<?php
function generateJsOpener(){
    
$result = "";
    
$urls   = getRowWithTitle("test.csv", "url");
    
$result .= "<script>var urls=[";
    foreach (
$urls as $url) {
        if (
parse_url($url) !== false) {
            
$result .= '"' . str_replace('"', "", $url) . '",';
        }
    }
    
$result = rtrim($result, "\",");
    
$result .= "\"];urls.forEach(function(entry) {window.open(entry,'_blank');});</script>";
    return 
$result;
}

function 
getRowWithTitle($file, $csvColumnTitle){
    
$result = array();
    
$csv    = getCSV($file);
    if (
count($csv) > 0) {
        
$pos = array_search($csvColumnTitle, $csv[0]);
        if (
$pos !== false) {
            for (
$i = 1; $i <= count($csv[0]); $i++) {
                
$result[] = $csv[$i][$pos];
            }
        }
    }
    return 
$result;
}

function 
getCSV($file){
    return 
array_map('str_getcsv', file($file));
}

echo 
generateJsOpener(); 



RE: How to parse csv file with php - jesus732 - Feb 26, 2019

Why don't use the phpoffice library?


RE: How to parse csv file with php - Abruptum - Feb 26, 2019

(Feb 26, 2019 04:19 PM)jesus732 Wrote:  Why don't use the phpoffice library?

Well its a big library and the most of its function are unused so its just a waste of space.
OP just needed a csv parser and we gave it to him.