IT79637
asked on
How to remove "\r\n" from text file?
Hi,
I'm on a Win XP Pro SP3 box, using Delphi 7 Ent & PHP 5.1.4.
I'm using Ultra Edit v14 as my text editor to create text based test files.
When reading a text file in Delphi and PHP , the following characters are at the end of each line: "\r\n".
How can I strip these characters once I've read them into a variable? Is there a MySQL function to strip the characters when doing a mySQL INSERT or UPDATE command?
A Delphi, PHP or MySQL solution would be perfect.
Thanks much!
I'm on a Win XP Pro SP3 box, using Delphi 7 Ent & PHP 5.1.4.
I'm using Ultra Edit v14 as my text editor to create text based test files.
When reading a text file in Delphi and PHP , the following characters are at the end of each line: "\r\n".
How can I strip these characters once I've read them into a variable? Is there a MySQL function to strip the characters when doing a mySQL INSERT or UPDATE command?
A Delphi, PHP or MySQL solution would be perfect.
Thanks much!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For MySQL, you can use REPLACE() also. You can use the CHAR(13) = Carriage Return or \r and CHAR(10) = Line Feed or \n.
REPLACE(your_column_or_str ing, '\r\n', '')
Where the last parameter is what you want to replace that combination with like empty string in example.
REPLACE(your_column_or_str
Where the last parameter is what you want to replace that combination with like empty string in example.
B-) Since you are using UltraEdit, you should convert the file from DOS to Unix within UE:
File / Conversions / DOS to Unix
This will automatically save the file to Unix end of line.
The different end of line between Windows, Unix/Linux and Max is a long time problem, with long time solutions:
- you can configure your FTP client so that it automatically convert ends of line between Windows and Linux: check that this happens for the text files you are using, eg .php and others
- in addition, using UE to convert end of line will work fine too
File / Conversions / DOS to Unix
This will automatically save the file to Unix end of line.
The different end of line between Windows, Unix/Linux and Max is a long time problem, with long time solutions:
- you can configure your FTP client so that it automatically convert ends of line between Windows and Linux: check that this happens for the text files you are using, eg .php and others
- in addition, using UE to convert end of line will work fine too
I have found that using the PHP file() function to read a file may give you array elements that contain extraneous end-of-line characters. Usually this is the result of an encoding error of some sort. It does no harm but you have to remove them. You can use an iterator like foreach to access each of the lines and you can use trim() or rtrim() to get rid of the whitespace.
When writing files with PHP, you might want to use the OS-context-aware end of line character given in the predefined constant PHP_EOL.
When writing files with PHP, you might want to use the OS-context-aware end of line character given in the predefined constant PHP_EOL.
procedure RemoveCRLF(myFile: string);
begin
with TStringList.Create do
try
LoadFromFile(myFile);
Text := StringReplace(Text, '\r\n', #13#10, [rfReplaceAll]);
SaveToFile(myFile);
finally
Free;
end;
end;
usage:
RemoveCRLF('myfile.txt');
Hope this helps...
begin
with TStringList.Create do
try
LoadFromFile(myFile);
Text := StringReplace(Text, '\r\n', #13#10, [rfReplaceAll]);
SaveToFile(myFile);
finally
Free;
end;
end;
usage:
RemoveCRLF('myfile.txt');
Hope this helps...
ASKER
My Code:
$line = str_replace("\r\n", "", $line);
Thanks much. Solution worked great!
$line = str_replace("\r\n", "", $line);
Thanks much. Solution worked great!
Please see http://www.w3schools.com/php/php_ref_string.asp