php.net |  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
Votes:1
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: admin at ifyouwantblood dot de
New email:
PHP Version: OS:

 

 [2008-01-10 19:06 UTC] admin at ifyouwantblood dot de
Description:
------------
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:
---------------
<?php

$something=strtok('something',':');
var_dump($something);

?>

Expected result:
----------------
bool(false)

Actual result:
--------------
string(9) "something"

Patches

Pull Requests

History

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

Index: string.c
===================================================================
RCS file: /repository/php-src/ext/standard/string.c,v
retrieving revision 1.445.2.14.2.69.2.8
diff -u -r1.445.2.14.2.69.2.8 string.c
--- string.c    31 Dec 2007 07:17:15 -0000      1.445.2.14.2.69.2.8
+++ 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)) {
 return_token:
                RETVAL_STRINGL(BG(strtok_last) + skipped, (p - BG(strtok_last)) - skipped, 1);
                BG(strtok_last) = p + 1;

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

    strtok('something', ':')

and

    strtok('', ':')

and

    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 (<https://wiki.php.net/rfc/howto>).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 15:01:31 2025 UTC