php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14541 strtok broken again
Submitted: 2001-12-16 02:15 UTC Modified: 2001-12-16 12:35 UTC
From: mlemos at acm dot org Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.1.0 OS: Any
Private report: No CVE-ID:
 [2001-12-16 02:15 UTC] mlemos at acm dot org
It seems that strtok function is broken again.

The following script returns:

<?
	$first_token=strtok("/something","/");
	$second_token=strtok("/");
	var_dump($first_token,$second_token);
?>

Should output as always (at least until PHP 4.0.6 it does):

string(0) ""
string(9) "something"

But it outputs:

string(9) "something"
bool(false)

It seems that jmoore broken in when he tried to fix this bug:

http://bugs.php.net/bug.php?id=13866

I think that no developer should be allowed to fix bugs before:

1) submit a test case to leave in the tests directory
2) Verify that his fixes do not make his and others tests fail.

Until this procedure becomes mandatory, we'll keep seeing a history of illness in functions like strtok that seems to never end.

This is what regressive tests are for. Zak, please help here! :-)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-16 02:25 UTC] zak@php.net
While we could try to force developers to write tests 
before they commit code (heck, MySQL does it), but we 
might not have much luck. :)

I think that we should look to the QA team (and interested 
individuals such as yourself) to start writing tests.

I am working on tests for the array functions right now 
(coincidentally, before I commit a whack of changes to the 
array functions. :)

In Frankfurt, Rasmus suggested that we develop a web-based 
interface for developing tests as a way to lower the 
barrier for writing tests. We could look at doing this.

 [2001-12-16 02:40 UTC] mlemos at acm dot org
I understand that it is very hard to make developers write tests for new software, but at least those that commit bug fixes should be required to submit test scripts that reproduce the bugs if they do not exist yet.

As for myself, I always present test cases when they are possible in the bug report itself, just like I did for this. So, developers have at least half of the job done.

I think that is a matter of making it a rule by adding to the CODING_STANDARDS.
 [2001-12-16 04:45 UTC] derick@php.net
jmoore did not break this, he fixed a faulty behavior that existed since 4.0.0.
The current behavior is the correct one, as outlined by every unix or windows manual about the topic 'strtok'.

Derick
 [2001-12-16 05:44 UTC] mfischer@php.net
Documention is now fixed in CVS, thx 2 derick.
 [2001-12-16 12:35 UTC] mlemos at acm dot org
No, jmoore just broke the behaviour of PHP strtok function that worked like that since PHP 3 for more than 4 years.

This means that he also broke PHP code that relied in that behaviour, thus discouraging people to upgrade to PHP 4.1.0 .

It does not matter what a Unix manual says because PHP users could not care less about Unix manuals when the behaviour of a function that they rely on changes without backwards compatibility.

 [2002-07-24 05:39 UTC] hax at hax dot at
I wholeheartly agree with mlemos !

This sucks big time!
A lot !!! of old scripts are NOT working anymore !
This sucks.

At least make a second function strtokold or something, that works like the old one, so it's easy to get the old scripts working again.

changing the way things work with no backwards compatibility simply sucks.
 [2002-07-24 06:41 UTC] hax at hax dot at
Nevermind my last posting.
I was able to find a workaround :)))
It would be cool if you would put this in the documentation I think !

To get the old strtok way:

put this in your php documents and replace strtok with strtokold

Function strtokold($string,$separator="") 
{ 
static $next_token=''; 
if(!strcmp($separator,"")) 
{ 
$separator=$string; 
$string=$next_token; 
} 
for($character=0;$character<strlen($separator);$character++) 
{ 
if(GetType($position=strpos($string,$separator[$character]))=="integer") 
{ 
$next_token=substr($string,$position+1); 
return(substr($string,0,$position)); 
} 
} 
$next_token=""; 
return($string); 
} 


tested and works FINE for me :)))

thanks a bunch.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 18:01:53 2014 UTC