Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.