1--TEST--
2Test strtok() function : usage variations - with embedded nulls in the strings
3--FILE--
4<?php
5/* Prototype  : string strtok ( str $str, str $token )
6 * Description: splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token
7 * Source code: ext/standard/string.c
8*/
9
10/*
11 * Testing strtok() : with embedded nulls in the strings
12*/
13
14echo "*** Testing strtok() : with embedded nulls in the strings ***\n";
15
16// defining varous strings with embedded nulls
17$strings_with_nulls = array(
18 		           "\0",
19 		           '\0',
20                           "hello\0world",
21                           "\0hel\0lo",
22                           "hello\0",
23                           "\0\0hello\tworld\0\0",
24                           "\\0he\0llo\\0",
25                           'hello\0\0'
26                           );
27
28// loop through each element of the array and check the working of strtok()
29// when supplied with different string values
30
31$counter = 1;
32foreach( $strings_with_nulls as $string )  {
33  echo "\n--- Iteration $counter ---\n";
34  var_dump( strtok($string, "\0") );
35  for($count = 1; $count <= 5; $count++)  {
36    var_dump( strtok("\0") );
37  }
38  $counter++;
39}
40
41
42echo "Done\n";
43?>
44--EXPECT--
45*** Testing strtok() : with embedded nulls in the strings ***
46
47--- Iteration 1 ---
48bool(false)
49bool(false)
50bool(false)
51bool(false)
52bool(false)
53bool(false)
54
55--- Iteration 2 ---
56string(2) "\0"
57bool(false)
58bool(false)
59bool(false)
60bool(false)
61bool(false)
62
63--- Iteration 3 ---
64string(5) "hello"
65string(5) "world"
66bool(false)
67bool(false)
68bool(false)
69bool(false)
70
71--- Iteration 4 ---
72string(3) "hel"
73string(2) "lo"
74bool(false)
75bool(false)
76bool(false)
77bool(false)
78
79--- Iteration 5 ---
80string(5) "hello"
81bool(false)
82bool(false)
83bool(false)
84bool(false)
85bool(false)
86
87--- Iteration 6 ---
88string(11) "hello	world"
89bool(false)
90bool(false)
91bool(false)
92bool(false)
93bool(false)
94
95--- Iteration 7 ---
96string(4) "\0he"
97string(5) "llo\0"
98bool(false)
99bool(false)
100bool(false)
101bool(false)
102
103--- Iteration 8 ---
104string(9) "hello\0\0"
105bool(false)
106bool(false)
107bool(false)
108bool(false)
109bool(false)
110Done
111