1--TEST--
2Test vsprintf() function : usage variations - string formats with strings
3--FILE--
4<?php
5/* Prototype  : string vsprintf(string format, array args)
6 * Description: Return a formatted string
7 * Source code: ext/standard/formatted_print.c
8*/
9
10/*
11 * Test vsprintf() when different string formats and string values are passed to
12 * the '$format' and '$args' arguments of the function
13*/
14
15echo "*** Testing vsprintf() : string formats with strings ***\n";
16
17
18// defining different heredoc strings
19$heredoc_string = <<<EOT
20This is string defined
21using heredoc.
22EOT;
23
24/* heredoc string with only numerics */
25$heredoc_numeric_string = <<<EOT
26123456 3993
274849 string
28EOT;
29
30/* empty heardoc string */
31$heredoc_empty_string = <<<EOT
32EOT;
33
34// defining array of string formats
35$formats = array(
36  "%s",
37  "%+s %-s %S",
38  "%ls %Ls, %4s %-4s",
39  "%10.4s %-10.4s %04s %04.4s",
40  "%'#2s %'2s %'$2s %'_2s",
41  "%% %%s %10 s%",
42  '%3$s %4$s %1$s %2$s'
43);
44
45// Arrays of string values for the format defined in $format.
46// Each sub array contains string values which correspond to each format string in $format
47$args_array = array(
48  array(" "),
49  array("hello\0world", "hello\0", "\0hello"),
50  array("@#$%&*", "@#$%&*", "\x55F", "\001"),
51  array("sunday", 'monday', "tuesday", 'wednesday'),
52  array($heredoc_string, "abcdef", $heredoc_numeric_string, $heredoc_empty_string),
53  array("one", "two", 'three', 'four'),
54  array("three", 'four', 'one', "two")
55
56);
57
58// looping to test vsprintf() with different string formats from the above $format array
59// and with string from the above $args_array array
60$counter = 1;
61foreach($formats as $format) {
62  echo "\n-- Iteration $counter --\n";
63  var_dump( vsprintf($format, $args_array[$counter-1]) );
64  $counter++;
65}
66
67echo "Done";
68?>
69
70--EXPECTF--
71*** Testing vsprintf() : string formats with strings ***
72
73-- Iteration 1 --
74string(1) " "
75
76-- Iteration 2 --
77string(19) "hello�world hello� "
78
79-- Iteration 3 --
80string(19) "@#$%&* s,   UF    "
81
82-- Iteration 4 --
83string(34) "      sund mond       tuesday wedn"
84
85-- Iteration 5 --
86string(71) "This is string defined
87using heredoc. abcdef 123456 3993
884849 string __"
89
90-- Iteration 6 --
91string(6) "% %s s"
92
93-- Iteration 7 --
94string(18) "one two three four"
95Done
96