Jump to content

[SOLVED] csv parsing by tab delimited


webent

Recommended Posts

Does anyone know of a way to parse a csv file, via php of course, that's tab delimited?

 

Here's what I am using, but it's still messing up...

 

<?
// Side note... there are 22 columns, not sure if I can use that data anywhere to ensure that it stops reading after that many columns... 
$row_counter = 0;

$handle = fopen("http://get-in-trouble-if-i-show.com/blah/vendor_export.php?use_link=on&ve_sid=505oyt8qdnq6d409m0vumxa0x5wu3itx", "r");

while (($fields = fgetcsv($handle, 0, " ", ",")) !== FALSE) { 
    $row_counter++;
    if ($row_counter == 1) { // First row header removed. 
                                }    else    {
// Remove apostrophes
$search = array("'");
$replace = array("");
$fields = str_replace($search,$replace,$fields);

echo $fields[0] . '<br>';
                                  
                                }
}
fclose($handle);
?>

 

Just doing the error checking now, once I see that all the fields are proper, then I'll be looping it in a db...

 

So, as you can see, I'm using " " and "," but I'm not sure how to tell it to look for "tabs" ...

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/111677-solved-csv-parsing-by-tab-delimited/
Share on other sites

The third parameter to fgetcsv is the delimiter, so you should use "\t" for a tab. You probably don't want to use the fourth parameter since that is for the string enclosure and that defaults to the double quote:

<?php
while (($fields = fgetcsv($handle, 0, "\t")) !== FALSE) { 
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.