Forum Moderators: coopster

Message Too Old, No Replies

Importing tab delimited file problem

         

smagdy

2:56 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Hello,

I have tab delimited file that is produced from a specific software, and i should work on it without re-saving it! I want to import it and save it to DB.. my problem is just in reading it!

Everything works fine when I open it by excel and re-save it as tab delimited (But I should not do so).

The problem in the original file is that I can't detect its new line and neither can PHP functions file() nor fgetcsv().

But if I opened it by excel, it opens correctly, each field on its own and each line on its own, so there must be newline sign there but I cant find it at all..

I am trying to echo the position of any newline value, i tried:

$handle = fopen($file, "r");
$data_rows = fgetcsv($handle, 0, "\t");
echo strpos($data_rows[0],"\n");exit;

\n
\r\n
<br>
<br />

So what could be the newline value?

jatar_k

3:30 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try opening the file in a text editor and take a look at what is actually in it

smagdy

3:46 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



i did and there is small square infront of the start of every newline.

And when i open it with wordpad, it displays correctly, ever line on its own..

So there must be a newline character but what could it be?

jatar_k

3:56 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



where is this file generated? does that give any clues to what it might be?

aside from locating the newline, have you tried just parsing the file?

does it act like it knows there are lines or does it just give you one big line?

smagdy

9:16 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



It is generated from a software, i have no much info about it!

Of course i parsed the file using file() and fgetcsv() but both read all the content in one array cell!

So how can excel read it properly but not PHP functions!

smagdy

9:24 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



For example this code displays all file content!

$data_rows = file($file);
$data_fields = explode("\n",$data_rows[0]);
echo $data_fields[0];exit;

Any ideas on what the newline sign could be?

smagdy

10:53 am on Nov 25, 2007 (gmt 0)

10+ Year Member



Another tip is that the file created on MAC OS...

I also used:
ini_set('auto_detect_line_endings', true);

But no effect!

jatar_k

2:10 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried exploding on \r

as opposed to explode did you try using functions that grab line by line from a file to see if they may be able to do it

you could try

file_get_contents
fgets
fgetcsv

or even just fread but I doubt that will work

the other thing is that it may not be a newline or carriage return at all, it could be some other char all together that excel seems to know

you could also look at the software that is producing it and see if there are settings for file type.

smagdy

2:45 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Thanks but nothing from these functions work as they should, just with that specific file though as i said b4, its read correctly by Excel..

jatar_k

2:55 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could try copying the char from a text editor, I have done that in the past

did you check the original software as I suggested?

smagdy

3:04 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



I have no access to that software, its a friend who I work with online that provide me with that file and he should check it more closely..

I tried now to copy the small square char that appears in the text editor and paste it in dreamweaver.. so it pasted a new line as if i hit enter!

So we agree that there is newline sign out there but not clear what is it!

jatar_k

3:08 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you try splitting on "\r"?

if you edit the script in a text editor you could try splitting on the weird char too

The Contractor

3:17 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using Open Office Calc to open and save. For some reason Excel screws up delimited files quite often with extra characters etc (same with csv files).

smagdy

3:22 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



did you try splitting on "\r"?
- Yes, didn't work!

if you edit the script in a text editor you could try splitting on the weird char too
- I did so and when I transfered the file by FTP, it lost the weird character and gave error (Empty delimiter)

jatar_k

3:32 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



since it it tab delim and has a set number of cols you could split them that way

loop it
add value to array
when you get to the number of cols start the next element

take a look in the comments on this page
[php.net...]

search the page for 'newline', might give some insight

smagdy

3:42 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Sorry but can you clear this part more?

loop it
add value to array
when you get to the number of cols start the next element

jatar_k

3:47 pm on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there are a set number of columns in the file, each needs to go to an array element, each row needs to be a new row in the array

for each value between tabs you could assign it to a new element

though I guess you would still need to figure out the strange char as there would be no tab there

no clue, you need to find a way to identify that char, the link above may help

smagdy

4:13 pm on Nov 25, 2007 (gmt 0)

10+ Year Member



Ok thanks, Also I will try to find that weird character and will inform u if I found anything!

Thanks again for your time!