/hphp/test/zend/good/ext/standard/tests/strings/strtr_variation3.php
PHP | 64 lines | 39 code | 10 blank | 15 comment | 2 complexity | e960e968c21b9cb98040615e1988397f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
- <?hh
- /* 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
- */
- <<__EntryPoint>> function main(): void {
- 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 = varray[
- //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 = darray["\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 ***";
- }