php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49208 strpos() does not match newlines at the end of $needle
Submitted: 2009-08-10 10:31 UTC Modified: 2009-08-10 21:09 UTC
From: emil at nachev dot eu Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.10 OS: Debian GNU/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: emil at nachev dot eu
New email:
PHP Version: OS:

 

 [2009-08-10 10:31 UTC] emil at nachev dot eu
Description:
------------
If $needle is a string that ends with a newline, strpos() does not find the string in the $haystack, although it is there.

Reproduce code:
---------------
$text = <<<TEXT
This is just some text, with 

something in the middle

that has newlines around it.
TEXT;

// The text contains both needles below:

$result = strpos($text, "\nsomething in the middle");
echo 'This works: '; var_dump($result);

echo "\n<br>\n";

$buggy_result = strpos($text, "something in the middle\n");
echo 'This does not: '; var_dump($buggy_result);

Expected result:
----------------
I expect matching for both "\nsomething in the middle" and "something in the middle\n" to return an integer. Matching for "something in the middle", without the newline, returns int(33) (as expected).


Actual result:
--------------
I get boolean false when matching for a string with "\n" at the end of $needle:

This works: int(32) 
This does not: bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-10 11:25 UTC] sjoerd-php at linuxonly dot nl
Thank you for your report.

I could not reproduce it. I get this instead:
This works: int(30)
This does not: int(31)

Note that you say you get int(33), where I get int(30). Both this and your example not working can be explained if you have Windows-style line endings in your file instead of Unix-style line endings. In other words, you have \r\n at the end of the line instead of \n. If this is the case, it is not a bug in PHP because middle\n does indeed not occur in the text.
 [2009-08-10 18:57 UTC] jani@php.net
Can't reproduce either. Check your data.
 [2009-08-10 21:09 UTC] emil at nachev dot eu
My bad, it was a browser issue. Firefox submits form data with \r\n for some reason. I didn't think to look for \r\n, because all testing and development is done on Debian.

Sorry to bother, and thanks for looking.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 06:01:35 2024 UTC