Page 1 of 1

Extracting data from text file

Posted: Thu Aug 10, 2006 9:35 am
by ratan
I am trying to load data from a pipe seperated text file into PHP script to ultimately load it into SQL Server DB.
I am trying to load the contents into an array, but that keeps on failing. Pls. suggest a solution to this.
Thanks.

Posted: Thu Aug 10, 2006 9:37 am
by JayBird
Show your code

Posted: Thu Aug 10, 2006 9:40 am
by ratan

Code: Select all

$handle = fopen(fileNameCreator("orders"), "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$dataArray = array();
$dataArray = explode(",", trim($contents));

Posted: Thu Aug 10, 2006 9:59 am
by blackbeard
If it's pipe seperated, you need to use:

Code: Select all

$dataArray = explode("|", trim($contents));

Posted: Thu Aug 10, 2006 10:03 am
by ratan
Yes, I am sorry. I know about that. We are going to change it to be seperated by | , but currently its seperated by " , " . So,
thats not the issue. But thanks.

Posted: Thu Aug 10, 2006 10:57 am
by blackbeard
Can you do a print_r($dataArray) and show us the results?

Posted: Thu Aug 10, 2006 11:47 am
by litebearer
Might try


Code: Select all

$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$dataArray = explode(",",$contents);
echo "<PRE>";
print_r($dataArray);
echo "</pre>";