note 21197 added to function.strtok
| From: | desolate19 at NOSPAM dot hotmail dot com | Date: | Thu, 02 May 2002 19:28:52 +0000 |
| Subject: | note 21197 added to function.strtok | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30100@lists.php.net to get a copy of this message | ||
Here is yet another explanation of strtok for the explode/split comments.
You can do things with strtok that you can't do with explode/split. explode breaks a string
using another string, split breaks a string using a regular expression. strtok breaks a string
using single _characters_ , but the best part is you can use multiple characters at the same time.
For example, if you are accepting user input and aren't sure how the user will decide to divide
up their data you could choose to tokenize on spaces, hyphens, slashes and backslashes ALL AT THE
SAME TIME:
<?PHP
$teststr = "blah1 blah2/blah3-blah4\\blah5";
$tok = strtok($teststr," /-\\");
while ($tok !== FALSE)
{
$toks[] = $tok;
$tok = strtok(" /-\\");
}
while (list($k,$v) = each($toks))
{
print ("$k => $v<BR>\n");
}
?>
/* OUTPUT:
0 => blah1
1 => blah2
2 => blah3
3 => blah4
4 => blah5
*/
You can't do that with explode, and this should be faster than using split because split uses
regular expressions.
And for the comments about explode/split putting your output into an array... as you can see,
it's not hard to work with arrays in PHP.
--
http://www.php.net/manual/en/function.strtok.php
http://master.php.net/manage/user-notes.php?action=edit+21197
http://master.php.net/manage/user-notes.php?action=delete+21197
http://master.php.net/manage/user-notes.php?action=reject+21197