?
Solved

Removing line breaks from the file

Posted on 2004-07-30
5
Medium Priority
?
274 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

743 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