PHP/String/strtok — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:07, 26 мая 2010
Содержание
Dividing a String into Tokens with strtok()
<?php
<html>
<head>
<title>Dividing a string into tokens with strtok()</title>
</head>
<body>
<div>
<?php
$test = "php?id=353&sec=44&user=harry&context=php";
$delims = "?&";
$word = strtok( $test, $delims );
while ( is_string( $word ) ) {
if ( $word ) {
print "$word<br/>";
}
$word = strtok( $delims );
}
?>
</div>
</body>
</html>
strtok() function performs a similar task to explode()
<?php
$anemail = "l@b.ca";
$thetoken = strtok ($anemail, "@");
while ($thetoken){
echo $thetoken . "<br />";
$thetoken = strtok ("@");
}
?>
strtok() function tokenizes string, using the characters specified in tokens.
Its syntax is: string strtok (string string, string tokens)
<?
$info = "WJ asdf asdf sdf";
// delimiters include colon (:), vertical bar (|), and comma (,)
$tokens = ":|,";
$tokenized = strtok($info, $tokens);
while ($tokenized) :
echo "Element = $tokenized<br>";
$tokenized = strtok ($tokens);
endwhile;
?>
strtok.php
<?php
$info = "this:is|a,test.";
$tokens = ":|,";
$tokenized = strtok($info, $tokens);
while ($tokenized) {
echo "Element = $tokenized<br>";
$tokenized = strtok($tokens);
}
?>