PHP/String/strtok

Материал из Web эксперт
Перейти к: навигация, поиск

Dividing a String into Tokens with strtok()

   <source lang="html4strict">

<?php <html> <head> <title>Dividing a string into tokens with strtok()</title> </head> <body>

<?php $test = "php?id=353&sec=44&user=harry&context=php"; $delims = "?&"; $word = strtok( $test, $delims ); while ( is_string( $word ) ) {

if ( $word ) {
  print "$word
"; } $word = strtok( $delims );

} ?>

</body> </html>

 </source>
   
  


strtok() function performs a similar task to explode()

   <source lang="html4strict">

<?php $anemail = "l@b.ca"; $thetoken = strtok ($anemail, "@"); while ($thetoken){

   echo $thetoken . "
"; $thetoken = strtok ("@");

} ?>

 </source>
   
  


strtok() function tokenizes string, using the characters specified in tokens.

   <source lang="html4strict">

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
"; $tokenized = strtok ($tokens);

endwhile; ?>

 </source>
   
  


strtok.php

   <source lang="html4strict">

<?php

  $info = "this:is|a,test.";
  $tokens = ":|,";
  $tokenized = strtok($info, $tokens);
  while ($tokenized) {
     echo "Element = $tokenized
"; $tokenized = strtok($tokens); }

?>

 </source>