Solved

Removing line breaks from the file

Posted on 2004-07-30
5
267 Views
Last Modified: 2008-02-26
Hi there,

I have a txt file which is generated in Mac with CRLF line breaks. Now, i am trying to read each line of this file in PC environment (Linux). But i couldn't do it yet, getting mad!

Could you please tell me how can i read from a file until the end of line... I am using the below code to read from the file but it reads all lines :(

$FileHandle = fopen("./mac.txt", "r");
      while (!feof($FileHandle))
            {
            // Read only first line
            $FirstLine = trim(fgets($FileHandle, 4096));
            }
fclose($FileHandle);

$FirstLine should include only the first line, but it includes all lines. How can i read a line from the file until the end of line (no matter CRLF, LF or CR)

Thanks!
0
Comment
Question by:blacklord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 total points
ID: 11674708
PHP can handle different line break formats transparently, but you need to enable it:

http://www.php.net/manual/en/ref.filesystem.php#ini.auto-detect-line-endings
0
 

Author Comment

by:blacklord
ID: 11674751
Thanks, it is great but i noticed that it works only PHP versions which are higher than PHP v4.3. Do you have any other solutions?

If no, i will accept this as an answer
0
 

Expert Comment

by:hrudolph
ID: 11674754
Wich version of PHP do you use? There were some problems with line endings, but afaik they fixed it in one of the last updates ...
0
 

Author Comment

by:blacklord
ID: 11674878
I am developing a PHP script which must work on multiple platforms with PHP > v4.1 :(

However, i think i will use Squinky's suggestion. Thanks!
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11674897
if you use a non-line oriented reading function, you can convert line breaks and explode them afterwards. I use this function for preprocessing all kinds of uploaded data - remember that form submissions often come with CRLF line breaks in too:

/**
* Normalise Mac LF and  Windows CRLF line breaks into UNIX CR line breaks
* @param string $text
* @return string
*/
function normalisebreaks($text) {
      return preg_replace('/\r\n|\n\r|\r/', "\n", $text);
}
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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