Page 1 of 1

newline character question

Posted: Thu May 22, 2003 2:20 pm
by liljester
i have a tab-delimited textfile that i want to import into a database. my problem is that there are some newline (\n or hex 0x0a) characters scattered throughout the file. I need to strip these newlines out, whilst keeping the other newline (\r\n or hex 0x0d + hex 0x0a) characters that mark the true line endings..

anyideas?

Re: newline character question

Posted: Thu May 22, 2003 2:25 pm
by slimsam1
liljester wrote:i have a tab-delimited textfile that i want to import into a database. my problem is that there are some newline (\n or hex 0x0a) characters scattered throughout the file. I need to strip these newlines out, whilst keeping the other newline (\r\n or hex 0x0d + hex 0x0a) characters that mark the true line endings..

anyideas?
Well, a sorta hackjob way would be to go through the file with str_replace and replace all the \r\n with some weird combination of characters, like ***___***, remove all of the remaining \n's, then replace all the ***___*** with \r\n again...

Posted: Thu May 22, 2003 2:34 pm
by liljester
ive tried to replace the carriage return (\r, hex 0x0d, ascii 13) but for some reason PHP refuses to do anything with the carriage return... i know that its in the file, because i opened it with a hex editor, and the chars i want out are "0x0a" and the line endings are "0x0d" followed by "0x0a".

if i try to do a str_replace(chr(13), "*********", $string) it does nothing.

WHOOOHOOOOO!

Posted: Thu May 22, 2003 3:05 pm
by liljester
Its fixed! *does a jig*

here's what the problem was....
$whole_file = fread("file.txt", "r");

should have been
$whole_file = fread("file.txt", "rb");
or
$whole_file = file_get_contents("file.txt");

hehe im not exaclty sure why the "b" (binary?) fixed the problem of php not being able to see the chr(13) character... but it did =)