[thelist] Reading a tab delimited file with php

Jackson Yee jyee at vt.edu
Mon Aug 19 20:51:01 CDT 2002


"CDitty" <mail at redhotsweeps.com> wrote in message
news:5.1.0.14.2.20020819202618.00bbb430 at redhotsweeps.com...
> Does anyone have any code examples on how to read and handle a tab
> delimited using PHP?  I can a comma delimited one, but I can't get the tab
> delimited to read right.

I don't know how you read a comma delimited file, Chris, but tab delimited
files shouldn't be any different except that you change the delimiter
character to '\t' rather than ','.  I'm guessing that you want something
along the lines of

function ReadCommaDelimitedFile($FileName, &$Data)
{
  $fd =& fopen($FileName, 'r');

  if (! $fd)
    return false;

  while (! feof($fd) )
  {
    $Line =& fgets($fd);

    $Values =& explode('\t', $Line);
    $LineArray = array();

    foreach ($Values as $Value)
    {
      $LineArray[] = $Value;
    }

    $Data[] = $LineArray;
  }

  fclose($fd);
  return true;
}

Afterwards, you can call the function as

ReadCommaDelimitedFile('foo.txt', $Bar);

echo 'Value 1 on line 1 is ', $Bar[0][0];

Usual disclaimers about writing code off of the top of my head apply. 8-)

--
Regards,
Jackson Yee
jyee at vt.edu
http://www.jacksonyee.com/





More information about the thelist mailing list