Solved

Removing line breaks from the file

Posted on 2004-07-30
5
259 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

830 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