PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/strings/strtr_variation3.php

http://github.com/facebook/hiphop-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
  1. <?hh
  2. /* Prototype : string strtr(string $str, string $from[, string $to]);
  3. string strtr(string $str, array $replace_pairs);
  4. * Description: Translates characters in str using given translation tables
  5. * Source code: ext/standard/string.c
  6. */
  7. /* Testing strtr() function by passing the
  8. * string containing various escape sequences for 'str' argument and
  9. * corresponding translation pair of chars for 'from', 'to' & 'replace_pairs' arguments
  10. */
  11. <<__EntryPoint>> function main(): void {
  12. echo "*** Testing strtr() : string containing escape sequences for 'str' arg ***\n";
  13. /* definitions of required input variables */
  14. $count = 1;
  15. $heredoc_str = <<<EOD
  16. \tes\t\\stt\r
  17. \\test\\\strtr
  18. \ntest\r\nstrtr
  19. \$variable
  20. \"quotes
  21. EOD;
  22. //array of string inputs for $str
  23. $str_arr = varray[
  24. //double quoted strings
  25. "\tes\t\\stt\r",
  26. "\\test\\\strtr",
  27. "\ntest\r\nstrtr",
  28. "\$variable",
  29. "\"quotes",
  30. //single quoted strings
  31. '\tes\t\\stt\r',
  32. '\\test\\\strtr',
  33. '\ntest\r\nstrtr',
  34. '\$variable',
  35. '\"quotes',
  36. //heredoc string
  37. $heredoc_str
  38. ];
  39. $from = "\n\r\t\\";
  40. $to = "TEST";
  41. $replace_pairs = darray["\n" => "t", "\r\n" => "T", "\n\r\t\\" => "TEST"];
  42. /* loop through to test strtr() with each element of $str_arr */
  43. for($index = 0; $index < count($str_arr); $index++) {
  44. echo "-- Iteration $count --\n";
  45. $str = $str_arr[$index]; //getting the array element in 'str' variable
  46. //strtr() call in three args syntax form
  47. var_dump( strtr($str, $from, $to) );
  48. //strtr() call in two args syntax form
  49. var_dump( strtr($str, $replace_pairs) );
  50. $count++;
  51. }
  52. echo "*** Done ***";
  53. }