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

Posted on 2011-10-06
Last Modified: 2012-05-12

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!
Question by:IT79637
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    \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
    LVL 4

    Accepted Solution

    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

    LVL 59

    Expert Comment

    by: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.
    LVL 29

    Expert Comment

    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
    LVL 107

    Expert Comment

    by:Ray Paseur
    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.
    LVL 1

    Expert Comment

    procedure RemoveCRLF(myFile: string);
      with TStringList.Create do
        Text := StringReplace(Text, '\r\n', #13#10, [rfReplaceAll]);



    Hope this helps...
    LVL 1

    Author Closing Comment

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

    Thanks much.  Solution worked great!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now