Home arrow XML arrow Page 3 - Talking business: How I Learned to Love CSV
XML

Talking business: How I Learned to Love CSV


One of the biggest issues Web developers have to tackle is not server failures, line outages or yet another annoying bug in a really popular browser. It is receiving content in a proper format and ensuring that the maintainers of our products will not mess around with our code too much. One way to do this is by giving those who maintain our products a file format they can handle. Chris Heilmann discusses the advantages of CSV, an Excel file format.

Author Info:
By: Chris Heilmann
Rating: 3 stars3 stars3 stars3 stars3 stars / 7
July 25, 2005
TABLE OF CONTENTS:
  1. · Talking business: How I Learned to Love CSV
  2. · Handing out files in different formats
  3. · So what is a CSV file and how can I use it?
  4. · Laziness as a benefit

print this article
SEARCH DEVARTICLES

Talking business: How I Learned to Love CSV - So what is a CSV file and how can I use it?
(Page 3 of 4 )

A CSV file is a comma separated list of data; each data set is on a new line. The first line contains the headers, explaining what the following elements are. In short, a CSV is a really easy table.

Saved as employees.csv, this is:

Name,Surname,Department,Room

Bob,Houston,Problems,214

Stephen,Seagull,Cleaning,231

Arnold,Egger,Security,102

Eliza,Ihaye,Communications,207

Back in the days of old, when we used Perl or ancient versions of PHP, we tackled CSV files with Regular Expressions, first splitting the data into an array at the linebreak, and then splitting the lines into data at the comma.

<?php

$data=load('employees.csv');

if($data!='')

{

            $lines=preg_split("/\n/",$data);

            $headers=preg_split("/,/",$lines[0]);

            echo '<table>';

            echo '<thead><tr>';

            foreach($headers as $h)

            {

                        echo '<th scope="col">'.$h.'</th>';

            }

            echo '</tr></thead>';

            echo '<tbody>';

            for($i=1;$i<sizeof($lines);$i++)

            {

                        $linedata=preg_split("/,/",$lines[$i]);

                        $class=$i%2==0?' class="odd"':'';

                        if($linedata[0]!='')

                        {

                                    echo '<tr'.$class.'>';

                                    foreach($linedata as $l)

                                    {

                                                echo '<td>'.$l.'</td>';

                                    }

                                    echo '</tr>';

                       }

            }

            echo '</tbody>';

            echo '</table>';

}

function load($filelocation)

{

            if (file_exists($filelocation))

            {

                        $newfile = fopen($filelocation,"r");

                        $file_content = fread($newfile, filesize($filelocation));

                        fclose($newfile);

                        return $file_content;

            }

}

?>

That works wonders, but soon reaches its limits, as there are dangers:

  • Unix and Windows environments do have different ways of defining a line break.
  • When an editor enters a comma in one of the fields, Excel will add quotation marks around that field data. We need to alter our regular expression to take care of that.
  • Excel offers various versions of CSV to save (Windows, DOS, Macintosh), each with different settings.

Lucky for us, the PHP developers showed mercy and created fgetcsv(), which tackles most of these issues. On Macintosh systems there might be some trouble still, but there is a workaround setting the auto_detect_line_endings in the PHP configuration to true .

Our previous example looks a lot easier when we use this function.

echo '<table>';

$row=0;

$handle = fopen("employees.csv", "r");

while (($data = fgetcsv($handle, 1000)) != FALSE)

            {

            if($row==0)

            {

                        echo '<thead><tr>';

                        foreach ($data as $c)

                        {

                                    echo '<th scope="col">'.$c.'</th>';

                        }

                        echo '</tr></thead><tbody>';

            } else {

                        $class=$row%2==0?' class="odd"':'';

                        echo '<tr'.$class.'>';

                        foreach ($data as $c)

                        {

                                    echo '<td>'.$c.'</td>';

                        }

                        echo '</tr>';

            }

            $row++;

}

fclose($handle);

echo '</tbody></table>';

For storing data or exporting it as a CSV, we can use the fputcsv() function. In connection with a proper header (content-type and content-disposition) it will prompt the user to either open our script output in Excel or save it.


blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials