...

View Full Version : Need help with import script



maghiel
04-04-2007, 02:02 PM
Oi,

I'm writing a script that imports lines from a plain text file to a database.
Now all values are seperated by a newline, but not given by \n but given by ASCII values CR and LF(13 and 10).
So I wrote this script:



<?php
$file = 'file.txt';

echo ('<pre>');
echo ('<b>Import script</b><br/>');

$handle = fopen($file, 'r');
$n = 0;
while(!feof($handle))
{
$n++;
$line = stream_get_line($handle, 4096, chr(13) . chr(10));
echo ('Line ' . $n . ': ' . $line . '<br/>');
}

echo ('</pre>');
?>


But that gives everything back as just one
Line 1: Bla
Bla
Bla
Bla


What am I doing wrong?

Fumigator
04-04-2007, 09:19 PM
Looks like it should work .... Try just chr(13) as your delimiter, see if you can get the lines separated, and then deal with the chr(10) using str_replace().

maghiel
04-05-2007, 08:49 AM
really weird, still won't split the lines and gives back in



Line 1: bla
blie
bloe
blee


Did a little workaround though: an explode on chr(10) and loop through the array.



<?php
$path = './';
$file = 'foo.txt';

echo ('<pre>');
echo ('<b>import</b><br/>');

$handle = fopen($path . $file, 'r');

while(!feof($handle))
{
$lines = stream_get_line($handle, 4096, chr(13));
$partners = explode(chr(10), $lines);
}

$n = 0;
foreach($partners as $partner)
{
echo ('Importing <b>' . $partner . '</b> ');
// Query stuff
}

echo ('<br/>');
echo ('All done, imported ' . $n . ' partners from ' . $file . '.');
echo ('</pre>');
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum