note 46089 added to function.strtok

From: Date: Wed, 29 Sep 2004 01:51:56 +0000
Subject: note 46089 added to function.strtok
Groups: php.notes 
Request: Send a blank email to php-notes+get-77549@lists.php.net to get a copy of this message
Be very careful with using strtok if there's any chance that you may be calling other functions that may use strtok as well. If any other function that you call while parsing the string decides to call strtok as well, it will clobber the internal string pointer being used by strtok and you may get unexpected results. Here's some code to explain what I mean: function parse_string2($string2) { for($tok = strtok($string2, '.'); $tok !== false; $tok = strtok(".")) { echo $tok; } } $string1 = "1.2.3.4.!.8.9"; $string2 = "5.6.7"; for($word = strtok($string1, '.'); $word !== false; $word = strtok(".")) { if ($word == '!') { echo parse_string2($string2); } else { echo $word; } } If I didn't know the internals of the function parse_string2 (say someone else develops that), but all I know is that parse_string2 should print out 567, then my expected output might be: 123456789 Instead, you only get: 1234567. It would be interesting if they could implement a strtok_r where you could explicitly denote which string to tokenize. ---- Manual Page -- http://www.php.net/manual/en/function.strtok.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46089 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46089&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+46089&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#77549) next »