Extracting data from text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ratan
Forum Commoner
Posts: 28
Joined: Thu Jul 13, 2006 4:22 pm

Extracting data from text file

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Show your code
ratan
Forum Commoner
Posts: 28
Joined: Thu Jul 13, 2006 4:22 pm

Post by ratan »

Code: Select all

$handle = fopen(fileNameCreator("orders"), "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$dataArray = array();
$dataArray = explode(",", trim($contents));
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

If it's pipe seperated, you need to use:

Code: Select all

$dataArray = explode("|", trim($contents));
ratan
Forum Commoner
Posts: 28
Joined: Thu Jul 13, 2006 4:22 pm

Post 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.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Can you do a print_r($dataArray) and show us the results?
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post 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>";
Post Reply