TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-30-2009, 01:20 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default Importing comma-delimited csv file, containing commas

I'm working on learning how a particular script works. The script imports a comma-delimited text file, and outputs the contents to an HTML table.

The file contains a name field, i.e., "Doe, John" (with comma).

The CSV files are comma-delimited, so I cannot use another character for the delimiter.

Is there a solution to this?

Thanks,
Dave
Dave is offline  
Reply With Quote
Old 10-30-2009, 01:47 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,216
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
I'm working on learning how a particular script works. The script imports a comma-delimited text file, and outputs the contents to an HTML table.

The file contains a name field, i.e., "Doe, John" (with comma).

The CSV files are comma-delimited, so I cannot use another character for the delimiter.

Is there a solution to this?

Thanks,
Dave
Encode the commas to their ASCII value (,). HTML pages will display this without any further modification.
Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Dave (10-30-2009)
Old 10-30-2009, 03:45 AM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks! I'll try it, but I know I'll have a follow-up question!
Dave is offline  
Reply With Quote
Old 10-30-2009, 11:59 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
The file contains a name field, i.e., "Doe, John" (with comma).
Maybe I missed it, but what's the problem? CSV fields can contain commas if they are enclosed in quotes (or some other defined character).

PHP Code:
print_r(str_getcsv('foo,"bar,baz"'));
/*
Array
(
    [0] => foo
    [1] => bar,baz
)
*/ 
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (10-30-2009)
Old 10-30-2009, 01:48 PM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Right. That's the problem.

The comma-delimited CSV file contains commas in the NAME field (e.g., "Doe, John"), but my delimiter in the program I'm studying is a "," (comma).

Thus, when the file is read, it produces the following in an HTML table:

PHP Code:
// NAME    ID     
// Doe     John      9999

// What I need is this:

// NAME           ID     
// Doe, John      9999 
Thanks.
Dave is offline  
Reply With Quote
Old 10-30-2009, 02:04 PM   #6 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Just wanted to make sure I'm on the right path. So, I did a "search & replace" in the CSV file and changed the commas in the name field to the ASCII value.

The HTML table then displayed the CORRECTLY-FORMATTED information, so this works.

However, is there a way to do this programmatically with with PHP as each line of the CSV file is read, so that I don't have to touch the CSV file?

Would posting the code help?

Thanks!

Dave
Dave is offline  
Reply With Quote
Old 10-30-2009, 02:22 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Can you give us a few lines of the CSV file showing the problem fields?
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
Old 10-30-2009, 02:48 PM   #8 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Here are a few lines below. Only 2 fields: a name field and an ID field. As you can see, the name field contains commas. When the program reads the CSV file, it generates a third field because it "thinks" that the comma within the name field is a field separator.

Thanks!


PHP Code:
/*

Wiley, Jimmie    963144
Johnson, Michelle    1009959
Lynn, Kenya    1183526
Johnson, Gregory    1189881
Dixon, Kendric    1189915

*/ 
Dave is offline  
Reply With Quote
Old 10-30-2009, 03:38 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

That data appears to be not comma-separated, but tab-separated. The commas shouldn't be an issue if you specify the correct delimiter (comma in CSV, tab in TSV, etc.).

PHP Code:
$handle fopen("test.tsv""r");
while ((
$row fgetcsv($handle1000"\t")) !== FALSE) {
    
print_r($row);
}
fclose($handle); 
Code:
Array
(
    [0] => Wiley, Jimmie
    [1] => 963144
)
Array
(
    [0] => Johnson, Michelle
    [1] => 1009959
)
Array
(
    [0] => Lynn, Kenya
    [1] => 1183526
)
Array
(
    [0] => Johnson, Gregory
    [1] => 1189881
)
Array
(
    [0] => Dixon, Kendric
    [1] => 1189915
)
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (10-30-2009)
Old 10-30-2009, 05:04 PM   #10 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

I'm a little confused. The tab delimiter is not working properly.

I'm attaching a csv file (mycsvfile.txt) which, I think, is comma-delimited. I'm still inappropriately getting 2 separate fields from the "name" field because it contains a comma.

Thanks!
Attached Files
File Type: txt mycsvfile.txt (168 Bytes, 48 views)
Dave is offline  
Reply With Quote
Old 10-30-2009, 08:16 PM   #11 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

That data is different to what you posted earlier and is indeed a normal CSV file. How are you parsing the data? fgetcsv should have no problem with it at all.

Does this work?

PHP Code:
<?php

$fp   
fopen("mycsvfile.txt","r");
$rows = array();
while ((
$row fgetcsv($fp)) !== FALSE) {
    
$rows[] = $row;
}
fclose($fp);
?>
<table>
 <thead>
  <tr>
   <td>Name</td>
   <td>ID</td>
  </tr>
 </thead>
 <tbody>
<?php foreach ($rows as $row) : list($name$id) = $row?>
  <tr>
   <td><?php echo $name?></td>
   <td><?php echo $id?></td>
  </tr>
<?php endforeach; ?>
 </tbody>
</table>
__________________
salathe@php.net
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (10-31-2009)
Old 10-31-2009, 12:18 AM   #12 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Yes, thanks very much. That works perfectly!

The script I was using to learn from used the fgets() function. Later in the script, the split() function was used, based on a comma delimiter.

So, in looking back, maybe the author intended the script to be used for only comma-delimited text files, and not comma-delimited csv files. Although, I sorta thought that a comma-delimited text file WAS a csv file, but maybe not...

Alright, well, this was a good learning experience! Thanks for the help.

Dave
Dave is offline  
Reply With Quote
Old 10-31-2009, 01:11 AM   #13 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 322
Thanks: 2
Enfernikus is on a distinguished road
Default

A CSV file is technically a comma delimited text file but a simple split on commas does not adhere to the full CSV standard.
__________________
My Blog
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
Dave (10-31-2009)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where is my file? superthin General 3 07-25-2009 09:48 AM
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 3 05-08-2009 03:22 PM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 07:17 PM
Using class methods in an included file? Andrew Advanced PHP Programming 6 12-23-2007 02:20 AM


All times are GMT. The time now is 09:56 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design