Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

How to parse csv file with php

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


[Image: 26yROWk.gif]
Learning to use Search Engines bro
https://stackoverflow.com/questions/9139...-using-php
(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
(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>";
    } 


[Image: 26yROWk.gif]
(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 ?
(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(); 
Why don't use the phpoffice library?
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by ОЯВІТMay 29, 2020 04:33 AM 9 341 May 29, 2020 04:33 AM
Last Post: Sugawara
[PHP] by BulgarianFeb 21, 2020 12:33 AM 5 374 Feb 21, 2020 12:33 AM
Last Post: Backbone
[Javascript]  by newt0nNov 22, 2019 05:24 AM 3 204 Nov 22, 2019 05:24 AM
Last Post: ModernJS