strtok 


string apg

TOKENIZE a STRING.





This function splits a $str into smaller STRINGS, (tokens), with each token being delimited by any character from $token.

If the provided $str has several spaces, it could be splitted, tokenized, in individual words using the space character as the token.

It is worth noting that only the first call to this function uses the $str argument.

Every subsequent call to this function only needs the TOKEN to use, as it keeps track of where it is in the current STRING.

To start over, or to tokenize a new STRING you simply call strtok with the STRING argument again to initialize it.

Multiple tokens are accepted in the $token parameter, like this, the $str will be tokenized when any one of the characters in the argument is found.



<?php

str strtok 
str $str str $token )


where,

$str The STRING being split up into tokens

$token 
The delimiter used when splitting up $str

?>
 

<?php

str strtok 
str $token )


where,

$token The delimiter used when splitting up a STRING

?>
 

$str


The STRING being split up into smaller STRINGS, (tokens).



$token


The delimiter used when splitting up $str.



  1 EXERCISE   

<?php

$str01 
'Every dog has his day.';

$tks01 ' ';

$strtk01 strtok($str01$tks01)
 . 
' ' strtok($tks01) . ' ' strtok($tks01)
 . 
' ' strtok($tks01) . ' ' strtok($tks01);

echo 
$strtk01 '<br>';

?> 

  2 EXERCISE   

<?php

$str02 
'https://php.net/manual/en/function.strtok.php';

echo 
$str02 '<br><br>';

$tks02 '\ /. ';

$strtk02 strtok($str02$tks02)
 . 
' ' strtok($tks02) . ' ' strtok($tks02)
 . 
' ' strtok($tks02) . ' ' strtok($tks02)
 . 
' ' strtok($tks02) . ' ' strtok($tks02);

echo 
$strtk02 '<br>';

?> 

  3 EXERCISE   

<?php

/*
 * Testing strtok() : basic functionality
*/

echo "Testing strtok() : basic functionality.<br>";

// Initialize all required variables
$str 'This testcase test strtok() function.';
$token ' ().';

echo 
"<br>The Input string is:<br>\"$str\"<br>";
echo 
'<br><br>';
echo 
"The token string is:<br>\"$token\"<br>";

// using strtok() with $str argument
echo "<br><br>Token 1;<br>";
var_dumpstrtok($str$token) );

for( 
$i 2$i <=7$i++ )  {
  echo 
"<br><br>Token $i:<br>";
  
var_dumpstrtok($token) );
}

?>

  4 EXERCISE   

<?php

/*
 * Testing strtok() : with heredoc strings
*/

echo "Testing strtok() : with heredoc strings.<br>";

// defining different heredoc strings

$empty_heredoc = <<<EOT
EOT;

$heredoc_with_newline = <<<EOT
\n

EOT;

$heredoc_with_characters = <<<EOT
first line of heredoc string
second line of heredoc string
third line of heredocstring
EOT;

$heredoc_with_newline_and_tabs = <<<EOT
hello\tworld\nhello\nworld\n
EOT;

$heredoc_with_alphanumerics = <<<EOT
hello123world456
1234hello\t1234
EOT;

$heredoc_with_embedded_nulls = <<<EOT
hello\0world\0hello
\0hello\0
EOT;

$heredoc_strings = [
                   
$empty_heredoc,
                   
$heredoc_with_newline,
                   
$heredoc_with_characters,
                   
$heredoc_with_newline_and_tabs,
                   
$heredoc_with_alphanumerics,
                   
$heredoc_with_embedded_nulls
                   
];

// loop through each element of the array 
// and check the working of strtok()
// when supplied with different string values

$count 1;
foreach(
$heredoc_strings as $string)  {
  echo 
"<br><br>Iteration: $count<br>";
  
var_dumpstrtok($string"5o\0\n\t") );
  echo 
'<br>';
  for(
$counter 1$counter <= 10$counter++)  {
    
var_dumpstrtok("5o\0\n\t") );
    echo 
'<br>';
    
  }
  
$count++;
}

?>

  5 EXERCISE   

<?php

/*
 * Testing strtok() : with embedded nulls in the strings
*/

echo "Testing strtok() : with embedded nulls in the strings.<br>";

// defining varous strings with embedded nulls
$strings_with_nulls = array(
                   
"\0",
                   
'\0',
                           
"hello\0world",
                           
"\0hel\0lo",
                           
"hello\0",
                           
"\0\0hello\tworld\0\0",
                           
"\\0he\0llo\\0",
                           
'hello\0\0'
                           
);

// loop through each element of the array 
// and check the working of strtok()
// when supplied with different string values

$counter 1;
foreach( 
$strings_with_nulls as $string )  {
  echo 
"<br><br>Iteration: $counter<br>";
  
var_dumpstrtok($string"\0") );
  echo 
'<br>';
  for(
$count 1$count <= 5$count++)  {
    
var_dumpstrtok("\0") );
    echo 
'<br>';
  }
  
$counter++;
}

?>

  6 EXERCISE   

<?php

/*
 * Testing strtok() : with miscellaneous 
 * combinations of string and token
*/

echo "Testing strtok() : with miscellaneous inputs.<br>";

// defining arrays for input strings and tokens
$string_array = array(
               
"HELLO WORLD",
               
"hello world",
               
"_HELLO_WORLD_",
               
"/thello/t/wor/ttld",
               
"hel/lo/t/world",
                       
"one:$:two:!:three:#:four",
               
"\rhello/r/wor\rrld",
                   
chr(0),
                       
chr(0).chr(0),
                       
chr(0).'hello'.chr(0),
                       
'hello'.chr(0).'world'
             
);
$token_array = array(
              
"wr",
              
"hello world",
              
"__",
                      
"t/",
              
'/t',
              
":",
              
"\r",
              
"\0",
              
"\0",
              
"\0",
              
"\0",
            );

// loop through each element of the array 
// and check the working of strtok()
// when supplied with different string 
// and token values

$counter =1;
foreach( 
$string_array as $string )  {
  echo 
"<br><br>Iteration: $counter<br>";
  
var_dumpstrtok($string$token_array[$counter-1]) );
  echo 
'<br>';
  for( 
$count 1$count <=5$count++ )  {
    
var_dumpstrtok($token_array[$counter-1]) );
    echo 
'<br>';
  }
  
$counter++;
}

?>

  7 EXERCISE   

<?php

/*
 * Testing strtok() : with invalid escape 
 * sequences in token
*/

echo "Testing strtok() : 
            with invalid escape sequences in token.<br>"
;

// defining arrays for input strings and tokens
$string_array = array(
               
"khellok worldk",
               
"\khello\k world\k",
               
"/khello\k world/k",
               
"/hellok/ world"
             
);
$token_array = array(
               
"k",
               
"/ ",
               
"/k",
               
"\k",
               
"\\\\\\\k\h\\e\l\o\w\r\l\d"
            
);

// loop through each element of the array 
// and check the working of strtok()
// when supplied with different string 
// and token values

$counter =1;
foreach( 
$string_array as $string )  {
  echo 
"<br><br>Iteration: $counter<br>";
  foreach( 
$token_array as $token )  {
    
var_dumpstrtok($string$token) );
    echo 
'<br>';
    for( 
$count 1$count <=3$count++ )  {
      
var_dumpstrtok($token) );
      echo 
'<br>';
    }
    echo 
"<br>";
  }
  
$counter++;
}

?>

  8 EXERCISE   

<?php

/*
 * Testing strtok() : modifying the input string 
 * while it is getting tokenised
*/

echo "Testing strtok() : 
            with modification of input string 
                           in between tokenising.<br>"
;

$str "this is a sample string";
$token " ";

echo 
"<br><br>Given:<br>\"$str\"<br>and<br>\"$token\"";

echo 
"<br><br>Testing strtok() when string being 
                   tokenised is prefixed with another 
                   string in between the process:<br>"
;
var_dumpstrtok($str$token) );
// adding a string to the input string 
// which is being tokenised
$str "extra string ".$str;
for( 
$count 1$count <=6$count++ )  {
  echo 
"<br><br>Token $count is: <br>";
  
var_dumpstrtok($token) );
  echo 
"<br>Input str is:<br>\"$str\"<br>";
}

echo 
"<br><br>Testing strtok() when string being
                       tokenised is suffixed with another 
                       string in between the process:<br>"
;
var_dumpstrtok($str$token) );
// adding a string to the input string 
// which is being tokenised
$str $str." extra string";
for( 
$count 1$count <=10$count++ )  {
  echo 
"<br><br>Token $count is;<br>";
  
var_dumpstrtok($token) );
}

?>