Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with import script

    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 Code:
    <?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($handle4096chr(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?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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().

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    really weird, still won't split the lines and gives back in

    Code:
    Line 1: bla
    blie
    bloe
    blee
    Did a little workaround though: an explode on chr(10) and loop through the array.

    PHP Code:
    <?php
    $path     
    './';
    $file    'foo.txt';

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

    $handle fopen($path $file'r');

    while(!
    feof($handle))
    {
        
    $lines       stream_get_line($handle4096chr(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>');
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •