How to remove "\r\n" from text file?


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!
Who is Participating?
CKY092Connect With a Mentor Commented:
If you want to actually remove the \r and \n, you can use a string replace function.
<? $string = str_replace("\r\n", "", $string); ?>

Open in new window

Hugh McCurdyCommented:
\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
Kevin CrossChief Technology OfficerCommented:
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.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Bernard S.Commented:
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
Ray PaseurCommented:
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.
procedure RemoveCRLF(myFile: string);
  with TStringList.Create do
    Text := StringReplace(Text, '\r\n', #13#10, [rfReplaceAll]);



Hope this helps...
IT79637Author Commented:
My Code:
  $line = str_replace("\r\n", "", $line);

Thanks much.  Solution worked great!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.