note 17496 added to function.strtok

From: Date: Tue, 11 Dec 2001 17:57:43 +0000
Subject: note 17496 added to function.strtok
Groups: php.notes 
Request: Send a blank email to php-notes+get-22508@lists.php.net to get a copy of this message
The example is unnecessarily confusing for beginners. 1) It is NOT strtok that fails when the returned string evaluates to false in conditional expression, it is the loop test. A correct test is while($tok !== false) 2) the same functionality (AS THE EXAMPLE) can be obtained with explode. Note that if you only need the first few tokens you can put a limit on explode!! read the manual :) array explode (string separator, string string [, INT LIMIT]) What you can NOT do with explode (or split) is changing the separator after a token is returned, as for example, when parsing a string along a simple format : $styleStr = "color:#FFFFFF;font-size:12"; $key = strtok($styleStr,":"); while ($key !== false){ $styleTab[$key]= strtok(";"); // do not change the target $key = strtok(":"); // string, just the separator list } $styleTab is array("color"=>"#FFFFFF","font-size"=>"12") If you need the remaining of the string do : $remaining = strtok(''); //(empty separator) Ivan -- http://www.php.net/manual/en/function.strtok.php http://master.php.net/manage/user-notes.php?action=edit+17496 http://master.php.net/manage/user-notes.php?action=delete+17496 http://master.php.net/manage/user-notes.php?action=reject+17496

« previous php.notes (#22508) next »