However, the
str parameter needs to be specified only once because the function keeps track of its
position in str until it either completely tokenizes str or a new str parameter is
specified. Its behavior is best explained via an example:
$info = "J. Gilmore:jason@example.com|Columbus, Ohio";
// delimiters include colon (:), vertical bar (|), and comma (,)
$tokens = ":|,";
$tokenized = strtok($info, $tokens);
// print out each element in the $tokenized array
while ($tokenized) {
echo "Element = $tokenized
";
// Don't include the first argument in subsequent calls.
$tokenized = strtok($tokens);
}
?>
This returns the following:
Element = J. Gilmore
Element = jason@example.com
Element = Columbus
Element = Ohio
Exploding a String Based on a Predefined Delimiter
The explode() function divides the string str into an array of substrings. Its prototype
follows:
array explode(string separator, string str [, int limit])
The original string is divided into distinct elements by separating it based on the
character separator specified by separator. The number of elements can be limited
with the optional inclusion of limit. Let??™s use explode() in conjunction with sizeof()
and strip_tags() to determine the total number of words in a given block of text:
262 CHAPTER 9 ?– STRINGS AND REGULAR EXPRESSIONS
$summary = <<< summary
In the latest installment of the ongoing Developer.
Pages:
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338