Reading text problem (carriage return character)

Discussion in 'PHP' started by coool, Jul 25, 2007.

  1. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Hi :)


    I'm using PHP to read row by row a text file and insert it in my database

    everything is working fine, BUT ! i've noticed that about 10 columns have a return charachter - that's making a problem because the php consider these return charachters as a new line (new row)

    example:

    apple 1 fruit
    orange 2 fruit
    bana
    na 1 fruit
    ___________________

    PHP:
        $output str_replace("\t""|"$data);
        
    $values explode("\n"$output,-1);
        foreach(
    $values as $row)
        {
                 
    $col explode("|"$row);
                 
    //... etc
        
    }
    how can i escape or replace the \n in the column value by a space !!!

    i tried to str_replcae all \n by a space

    but that doesn't work as this removes all \n i.e there's only one row to read which contains all the data
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Use fscanf,

    PHP:
    $handle fopen("users.txt""r");
    while (
    $userinfo fscanf($handle"%s\t%s\t%s\n")) {
        list (
    $name$profession$countrycode) = $userinfo;
        
    //... do something with the values
    }
    fclose($handle);
     
  3. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    oh what i've given you is a small example

    I actually have 46 columns and 30000 rows
    so should i do this ??? while "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n !!?????
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    specify the format for each record (or each line)
     
  5. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    okay the last record of every line is a number (not a character)

    i.e. why don't i say... replace all \n that is after a character with a space !!

    I'm trying to form the regx !!

    this doesn't work but it's near !

    PHP:
        $data preg_replace("\[a-zA-Z]\\n\$""\\s"$data);
    any help in forming the regx please !?
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    PHP:
    $data preg_replace("/([^\d]+)\\n/""$1 "$data); 
     
  7. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    oh no

    I don't want to replace the final \n

    i want to replace the \n that is inside one of the values by a space

    :)
     
    Last edited: Jul 25, 2007
  8. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    imagine that this is the example

    apple 1 fruit 74
    orange 2 fruit 213
    bana
    na 1 fruit 345


    so I'm sure that the final \n have a number before it..

    but i don't know about the value !!

    so assuming that the value have only characters

    i want to replace any \n or \r found inside that value by a space !!
     
  9. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    I've solved the problem

    thanks :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice