note 21197 deleted from function.strtok by googleguy

From: Date: Tue, 22 Oct 2013 06:50:26 +0000
Subject: note 21197 deleted from function.strtok by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196667@lists.php.net to get a copy of this message
Note Submitter: desolate19 at NOSPAM dot hotmail dot com ---- 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&lt;BR&gt;\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.

« previous php.notes (#196667) next »