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