1--TEST--
2Test strtr() function : usage variations - string containing escape sequences for 'str' argument
3--FILE--
4<?php
5/* Testing strtr() function by passing the
6 *   string containing various escape sequences for 'str' argument and
7 *   corresponding translation pair of chars for 'from', 'to' & 'replace_pairs' arguments
8*/
9
10echo "*** Testing strtr() : string containing escape sequences for 'str' arg ***\n";
11/* definitions of required input variables */
12$count = 1;
13
14$heredoc_str = <<<EOD
15\tes\t\\stt\r
16\\test\\\strtr
17\ntest\r\nstrtr
18\$variable
19\"quotes
20EOD;
21
22//array of string inputs for $str
23$str_arr = array(
24  //double quoted strings
25  "\tes\t\\stt\r",
26  "\\test\\\strtr",
27  "\ntest\r\nstrtr",
28  "\$variable",
29  "\"quotes",
30
31  //single quoted strings
32  '\tes\t\\stt\r',
33  '\\test\\\strtr',
34  '\ntest\r\nstrtr',
35  '\$variable',
36  '\"quotes',
37
38  //heredoc string
39  $heredoc_str
40);
41
42$from = "\n\r\t\\";
43$to = "TEST";
44$replace_pairs = array("\n" => "t", "\r\n" => "T", "\n\r\t\\" => "TEST");
45
46/* loop through to test strtr() with each element of $str_arr */
47for($index = 0; $index < count($str_arr); $index++) {
48  echo "-- Iteration $count --\n";
49
50  $str = $str_arr[$index];  //getting the array element in 'str' variable
51
52  //strtr() call in three args syntax form
53  var_dump( strtr($str, $from, $to) );
54
55  //strtr() call in two args syntax form
56  var_dump( strtr($str, $replace_pairs) );
57
58  $count++;
59}
60echo "*** Done ***";
61?>
62--EXPECT--
63*** Testing strtr() : string containing escape sequences for 'str' arg ***
64-- Iteration 1 --
65string(9) "SesSTsttE"
66string(9) "	es	\stt
66"
67-- Iteration 2 --
68string(12) "TtestTTstrtr"
69string(12) "\test\\strtr"
70-- Iteration 3 --
71string(12) "TtestETstrtr"
72string(11) "ttestTstrtr"
73-- Iteration 4 --
74string(9) "$variable"
75string(9) "$variable"
76-- Iteration 5 --
77string(7) ""quotes"
78string(7) ""quotes"
79-- Iteration 6 --
80string(12) "TtesTtTsttTr"
81string(12) "\tes\t\stt\r"
82-- Iteration 7 --
83string(12) "TtestTTstrtr"
84string(12) "\test\\strtr"
85-- Iteration 8 --
86string(15) "TntestTrTnstrtr"
87string(15) "\ntest\r\nstrtr"
88-- Iteration 9 --
89string(10) "T$variable"
90string(10) "\$variable"
91-- Iteration 10 --
92string(8) "T"quotes"
93string(8) "\"quotes"
94-- Iteration 11 --
95string(54) "SesSTsttETTtestTTstrtrTTtestETstrtrT$variableTT"quotes"
96string(52) "	es	\sttT\test\\strtrtttestTstrtrt$variablet\"quotes"
97*** Done ***
98