|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #43809 strtok() should return FALSE if needle wasnt found
Submitted: 2008-01-10 19:06 UTC Modified: 2017-09-21 16:08 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: admin at ifyouwantblood dot de Assigned: cmb (profile)
Status: Closed Package: Strings related
PHP Version: 5.2.5 OS: Any
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: admin at ifyouwantblood dot de
New email:
PHP Version: OS:


 [2008-01-10 19:06 UTC] admin at ifyouwantblood dot de
In PHP 4.1 strtok() has been changed to return FALSE if a repeated token wasnt found in the haystack string. It should also return FALSE if needle wasnt found at first place.

Reproduce code:



Expected result:

Actual result:
string(9) "something"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-11 00:09 UTC]
Looks fine to me. My try:

Index: string.c
RCS file: /repository/php-src/ext/standard/string.c,v
retrieving revision 1.445.
diff -u -r1.445. string.c
--- string.c    31 Dec 2007 07:17:15 -0000      1.445.
+++ string.c    11 Jan 2008 00:00:34 -0000
@@ -1264,7 +1264,7 @@
-       if (p - BG(strtok_last)) {
+       if ((skipped || BG(strtok_len) != strlen(BG(strtok_last))) && p - BG(strtok_last)) {
                RETVAL_STRINGL(BG(strtok_last) + skipped, (p - BG(strtok_last)) - skipped, 1);
                BG(strtok_last) = p + 1;

 [2011-04-08 21:21 UTC]
-Package: Feature/Change Request +Package: Strings related
 [2017-09-21 16:08 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2017-09-21 16:08 UTC]
Besides that PHP's strtok() basically follows the POSIX specification, changing
the behavior as suggested would make the result ambiguous, because

    strtok('something', ':')


    strtok('', ':')


    strtok(':::', ':')

would all return FALSE, and it wouldn't be easy to distinguish these cases.

I don't think changing the behavior would be a good idea. If you think otherwise
please feel free to start the RFC process (<>).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 07:01:29 2024 UTC