Link to home
Start Free TrialLog in
Avatar of IT79637
IT79637Flag for United States of America

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!
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

\r and \n are whitespace.  You can use string functions in php to strip whitespace.  The function trim() will strip white space from both ends of the string.  rtrim() will strip from just the right side of the string.

Please see   http://www.w3schools.com/php/php_ref_string.asp
ASKER CERTIFIED SOLUTION
Avatar of CKY092
CKY092
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kevin Cross
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_string, '\r\n', '')

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
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.
Avatar of karagunes
karagunes

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...
Avatar of IT79637

ASKER

My Code:
  $line = str_replace("\r\n", "", $line);

Thanks much.  Solution worked great!