php-src/ext/standard/tests/strings/strtr_variation3.phpt
Peter Kokot b746e69887 Sync leading and final newlines in *.phpt sections
This patch adds missing newlines, trims multiple redundant final
newlines into a single one, and trims redundant leading newlines in all
*.phpt sections.

According to POSIX, a line is a sequence of zero or more non-' <newline>'
characters plus a terminating '<newline>' character. [1] Files should
normally have at least one final newline character.

C89 [2] and later standards [3] mention a final newline:
"A source file that is not empty shall end in a new-line character,
which shall not be immediately preceded by a backslash character."

Although it is not mandatory for all files to have a final newline
fixed, a more consistent and homogeneous approach brings less of commit
differences issues and a better development experience in certain text
editors and IDEs.

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
[2] https://port70.net/~nsz/c/c89/c89-draft.html#2.1.1.2
[3] https://port70.net/~nsz/c/c99/n1256.html#5.1.1.2
2018-10-15 04:32:30 +02:00

104 lines
2.5 KiB
PHP

--TEST--
Test strtr() function : usage variations - string containing escape sequences for 'str' argument
--FILE--
<?php
/* Prototype : string strtr(string $str, string $from[, string $to]);
string strtr(string $str, array $replace_pairs);
* Description: Translates characters in str using given translation tables
* Source code: ext/standard/string.c
*/
/* Testing strtr() function by passing the
* string containing various escape sequences for 'str' argument and
* corresponding translation pair of chars for 'from', 'to' & 'replace_pairs' arguments
*/
echo "*** Testing strtr() : string containing escape sequences for 'str' arg ***\n";
/* definitions of required input variables */
$count = 1;
$heredoc_str = <<<EOD
\tes\t\\stt\r
\\test\\\strtr
\ntest\r\nstrtr
\$variable
\"quotes
EOD;
//array of string inputs for $str
$str_arr = array(
//double quoted strings
"\tes\t\\stt\r",
"\\test\\\strtr",
"\ntest\r\nstrtr",
"\$variable",
"\"quotes",
//single quoted strings
'\tes\t\\stt\r',
'\\test\\\strtr',
'\ntest\r\nstrtr',
'\$variable',
'\"quotes',
//heredoc string
$heredoc_str
);
$from = "\n\r\t\\";
$to = "TEST";
$replace_pairs = array("\n" => "t", "\r\n" => "T", "\n\r\t\\" => "TEST");
/* loop through to test strtr() with each element of $str_arr */
for($index = 0; $index < count($str_arr); $index++) {
echo "-- Iteration $count --\n";
$str = $str_arr[$index]; //getting the array element in 'str' variable
//strtr() call in three args syntax form
var_dump( strtr($str, $from, $to) );
//strtr() call in two args syntax form
var_dump( strtr($str, $replace_pairs) );
$count++;
}
echo "*** Done ***";
?>
--EXPECT--
*** Testing strtr() : string containing escape sequences for 'str' arg ***
-- Iteration 1 --
string(9) "SesSTsttE"
string(9) " es \stt
"
-- Iteration 2 --
string(12) "TtestTTstrtr"
string(12) "\test\\strtr"
-- Iteration 3 --
string(12) "TtestETstrtr"
string(11) "ttestTstrtr"
-- Iteration 4 --
string(9) "$variable"
string(9) "$variable"
-- Iteration 5 --
string(7) ""quotes"
string(7) ""quotes"
-- Iteration 6 --
string(12) "TtesTtTsttTr"
string(12) "\tes\t\stt\r"
-- Iteration 7 --
string(12) "TtestTTstrtr"
string(12) "\test\\strtr"
-- Iteration 8 --
string(15) "TntestTrTnstrtr"
string(15) "\ntest\r\nstrtr"
-- Iteration 9 --
string(10) "T$variable"
string(10) "\$variable"
-- Iteration 10 --
string(8) "T"quotes"
string(8) "\"quotes"
-- Iteration 11 --
string(54) "SesSTsttETTtestTTstrtrTTtestETstrtrT$variableTT"quotes"
string(52) " es \sttT\test\\strtrtttestTstrtrt$variablet\"quotes"
*** Done ***