1--TEST--
2Test strtr() function : usage variations - string containing special chars 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 special characters for 'str' argument and
13 *   corresponding translation pair of chars for 'from', 'to' & 'replace_pairs' arguments
14*/
15
16echo "*** Testing strtr() : string containing special chars for 'str' arg ***\n";
17
18/* definitions of required input variables */
19$count = 1;
20
21$heredoc_str = <<<EOD
22%
23#$*&
24text & @()
25EOD;
26
27//array of string inputs for $str
28$str_arr = array(
29  //double quoted strings
30  "%",
31  "#$*",
32  "text & @()",
33
34  //single quoted strings
35  '%',
36  '#$*',
37  'text & @()',
38
39  //heredoc string
40  $heredoc_str
41);
42
43$from = "%#$*&@()";
44$to = "specials";
45$replace_pairs = array("$" => "%", "%" => "$", "#*&@()" => "()@&*#");
46
47/* loop through to test strtr() with each element of $str_arr */
48for($index = 0; $index < count($str_arr); $index++) {
49  echo "-- Iteration $count --\n";
50
51  $str = $str_arr[$index];  //getting the array element in 'str' variable
52
53  //strtr() call in three args syntax form
54  var_dump( strtr($str, $from, $to) );
55
56  //strtr() call in two args syntax form
57  var_dump( strtr($str, $replace_pairs) );
58
59  $count++;
60}
61echo "*** Done ***";
62?>
63--EXPECTF--
64*** Testing strtr() : string containing special chars for 'str' arg ***
65-- Iteration 1 --
66string(1) "s"
67string(1) "$"
68-- Iteration 2 --
69string(3) "pec"
70string(3) "#%*"
71-- Iteration 3 --
72string(10) "text i als"
73string(10) "text & @()"
74-- Iteration 4 --
75string(1) "s"
76string(1) "$"
77-- Iteration 5 --
78string(3) "pec"
79string(3) "#%*"
80-- Iteration 6 --
81string(10) "text i als"
82string(10) "text & @()"
83-- Iteration 7 --
84string(17) "s
85peci
86text i als"
87string(17) "$
88#%*&
89text & @()"
90*** Done ***
91