PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/facebook/hiphop-php
PHP | 55 lines | 30 code | 8 blank | 17 comment | 1 complexity | 2811bf2b95e6ed0c05a89224978ad81a 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. * combination of numeric & regular strings 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() : numeric & regular double quoted strings ***\n";
  13. /* definitions of required input variables */
  14. $count = 1;
  15. $heredoc_str = <<<EOD
  16. 123
  17. abc
  18. 1a2b3c
  19. EOD;
  20. //array of string inputs for $str
  21. $str_arr = varray[
  22. //double quoted strings
  23. "123",
  24. "abc",
  25. "1a2b3c",
  26. //single quoted strings
  27. '123',
  28. 'abc',
  29. '1a2b3c',
  30. //heredoc string
  31. $heredoc_str
  32. ];
  33. $from = "123abc";
  34. $to = "abc123";
  35. $replace_pairs = darray["1" => "a", "a" => 1, "2b3c" => "b2c3", "b2c3" => "3c2b"];
  36. /* loop through to test strtr() with each element of $str_arr */
  37. for($index = 0; $index < count($str_arr); $index++) {
  38. echo "-- Iteration $count --\n";
  39. $str = $str_arr[$index]; //getting the $str_arr element in $str variable
  40. //strtr() call in three args syntax form
  41. var_dump( strtr($str, $from, $to) );
  42. //strtr() call in two args syntax form
  43. var_dump( strtr($str, $replace_pairs) );
  44. $count++;
  45. }
  46. echo "*** Done ***";
  47. }