Solved

Removing line breaks from the file

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Testing a string condition in PHP 6 23
google map tracking website 5 24
website maintenance mode 1 17
MySQL Sub-Select Query Returning Duplicate Result 7 29
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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