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?
newline character question
Moderator: General Moderators
Re: newline character question
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...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?
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.
if i try to do a str_replace(chr(13), "*********", $string) it does nothing.
WHOOOHOOOOO!
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 =)
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 =)