How to identify in the CSV file whether the line ends with "\n","\r" or "\n\r"

Posted on 2013-09-05
Medium Priority
Last Modified: 2013-09-09

How to identify in the CSV file whether the line ends with "\n","\r" or "\n\r"

Since in mac it takes as CR as the new line.
In Windows CRLf as the new line.
In Linux Lf. as the new line.

I have a csv file which is created in mac,linux or windows it is unknown,i need to import the file.
Question by:Rocking
LVL 27

Assisted Solution

by:Cornelia Yoder
Cornelia Yoder earned 498 total points
ID: 39468059
Have you tried using PHP_EOL?
LVL 11

Assisted Solution

by:Paul S
Paul S earned 501 total points
ID: 39468067
here is a good sample form php.net for splitting something using a list of delimiters. just substitute \n, \r, \n\r

Here is my approach to have exploded output with multiple delimiter.


//$delimiters has to be array
//$string has to be array

function multiexplode ($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);


//And output will be like this:
// Array
// (
//    [0] => here is a sample
//    [1] =>  this text
//    [2] =>  and this will be exploded
//    [3] =>  this also
//    [4] =>  this one too
//    [5] => )
// )

LVL 111

Accepted Solution

Ray Paseur earned 501 total points
ID: 39468508
You can use file().  All of the PHP functions are documented on the PHP.net web site.  You might use FILE_IGNORE_NEW_LINES as a flag.  But a better approach would be to post your test data set here with the question.  You can post the information in the code snippet or post a link to your test files.  Then we can write the programming that will handle the end of line characters and show you exactly how it's done.

Featured Post

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.

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.

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 i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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.

621 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