php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49228 fgets returns 2 lines
Submitted: 2009-08-12 02:19 UTC Modified: 2009-08-15 21:26 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: admin at kthxbai2u dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: admin at kthxbai2u dot com
New email:
PHP Version: OS:

 

 [2009-08-12 02:19 UTC] admin at kthxbai2u dot com
Description:
------------
When I use fgets() it returns the line, and the next one...

Correct me if I am wrong, but when you ask for one line, it should return one line?

There is not even a function (that I have found yet) that returns ONLY 1 line...

fgets should not be adding a /r/n at the end, that should be up to the programmer...

Reproduce code:
---------------
---
From manual page: function.fgetss
---

$fp = fopen("AD_SYSTEM/popup.ads",'r'); 

while($line=fgets($fp)) 
{ 
//add the link text to the array
$ads[] = $line;
}

 
fclose($fp); 

echo $ads[1];

Expected result:
----------------
"http://somepopup.url/advertisement.html" (without quotes)

Actual result:
--------------
"http://somepopup.url/advertisement.html
" (without quotes, notice the 2nd line)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-12 02:31 UTC] admin at kthxbai2u dot com
I was looking at fread() just now, but it requires a length...

I dont want to specify a length.

I am looking for the equivalent of readLn() from some other language i know (forget which one) where it reads the entire line, and only outputs that one line... Meaning there is no new line after the line I requested...
 [2009-08-12 02:36 UTC] rasmus@php.net
As per the docs, fgets() returns the newline character that it reads.  That doesn't mean there are 2 lines.  It is a single line terminated by a newline character.  If you don't want that newline character, trim() it off.
 [2009-08-15 21:26 UTC] admin at kthxbai2u dot com
You can use trim with no args and it cuts the newline out?

See I was used to the newline not being there... And no one has mentioned that method anywhere... I googled for that for hours on end and there are always people asking that but no solutions...

Thanks, now I can resume that part of my work! <3 PHP
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 15 21:00:03 2025 UTC