Jump to content

Reading text problem (carriage return character)


coool

Recommended Posts

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

___________________

 

    $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

Link to comment
Share on other sites

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 !!

 

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 !

 

    $data = preg_replace("/\[a-zA-Z]\\n\$/", "\\s", $data);

 

any help in forming the regx please !?

Link to comment
Share on other sites

try specifying a newline character that immediately proceeds any non-digit, rather than specifying that it proceed a letter:

 

$data = preg_replace("/([^0-9])\\n/", "$1\\s", $data);

 

i'm not always spot-on the first time with regex, so i can't make any promises.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.