Here's a PHP function that reads the Half.com data file and returns it as an array.
There is a PHP function to read CSV files: fgetscv(), but it turns out that function doesn't read CSV files produced by Excel.
Note: a new version of this CSV class is posted to the PHP notebook.
The code needs some revision.
<?php
include('CSV.class.php');
function readHDCFile($path)
{
$o = array();
$p = new CSV();
$fh = fopen($path,'r');
fgets($fh); // get rid of the first line
while ($data = fgets($fh))
{
$row = $p->textToArray( $data );
$o[$row[5]] = array(
'ItemID' => $row[0],
'ISBN' => $row[4],
'Title' => $row[5],
'Category' => $row[8],
'Price' => $row[10],
'Condition' => $row[11],
'Notes' => $row[12],
'URL' => 'http://shops.half.ebay.com/declutter69_W0QQ'
);
}
return $o;
}
CSV.class.php:
<?php
define('CSV_TAB',"\t");
class CSV {
var $template;
/**
* @param array $template
*
* The $template is an array used to selectively quote fields.
* If the Nth element is 'quote', the Nth field will be quoted.
* For example, array('','','quote') causes the 3rd field to be
* quoted.
*/
function CSV( $template = NULL )
{
$this->template = $template;
}
function arrayToText( &$ar, $separator=',' )
{
$row = array();
reset($ar);
$count=0;
foreach($ar as $field)
{
if ($this->template[$count] != 'quote')
{
$field = '"'.$this->quote($field).'"';
}
$row[] = $field;
$count++;
}
return join($separator,$row);
}
/**
* Parses one line of a csv file.
*/
function &textToArray( $str, $separator=',' )
{
$out = array();
while($str)
{
if (preg_match('/^"/', $str))
{
if (preg_match("/\"(.+?)\"$separator(.+)$/", $str, $matches))
{
$head = $this->dequote($matches[1]);
$str = $matches[2];
}
else // assume it's the last element
{
$head = $this->dequote($str);
$str = '';
}
}
else
{
if (preg_match("/^$separator/",$str))
{
// this is a special case of a null field
// it's exceptional, because the . metachar matches
// non-whitespace, and our separator might be whitespace
$head = '';
$str = substr($str,1);
}
else if (preg_match("/(.+?)$separator(.+)$/", $str, $matches))
{
$head = $matches[1];
$str = $matches[2];
}
else // assume it's the last element
{
$head = $str;
$str = '';
}
}
$out[] = $head;
}
return $out;
}
function quote( $s )
{
$s = preg_replace('/"/','""', $s);
return $s;
}
function dequote( $s )
{
$s = preg_replace('/""/','"', $s);
return $s;
}
}