PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/2.0/Tests/Strings/str_replace.php

#
PHP | 56 lines | 45 code | 8 blank | 3 comment | 0 complexity | d2f5cd855be2d545fe38235886b42b9c MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. [expect php]
  2. [file]
  3. <?
  4. function da($a)
  5. {
  6. foreach($a as $k => $v) echo "$k => $v\n";
  7. }
  8. // 8 combinations of possible str_replace arguments:
  9. $arr1 = array(
  10. "a" => "hello",
  11. "b" => "world",
  12. "c" => "earth",
  13. "d" => "europe",
  14. "e" => "africa",
  15. "f" => "america",
  16. "g" => "asia",
  17. "h" => "eheheheheheijijijijijlololololo",
  18. "i" => "klpofklpofklpof",
  19. "j" => "www",
  20. "k" => "lo",
  21. "l" => "as",
  22. "m" => "ing",
  23. "n" => "y",
  24. "o" => "string");
  25. $arr2 = array(
  26. "h" => "eheheheheheijijijijijlololololo",
  27. "i" => "klpofklpofklpof",
  28. "j" => "www",
  29. "k" => "lo",
  30. "l" => "as",
  31. "m" => "ing",
  32. "n" => "y");
  33. $str = "hello, very long string with several substrings to be replaced or removed";
  34. echo str_replace(array("hello", "string", "very", "long"), array("XXX"), $str), "\n";// replace only the first needle with XXX
  35. echo str_replace(array("hello", "string", "very", "long"), "XXX", $str), "\n"; // replace all the needles with the same replacement
  36. echo str_replace("","eh, nothing will happen",$str),"\n";
  37. echo str_replace("ing","",$str),"\n";
  38. //echo str_replace("remo",$arr1,$str),"\n";
  39. echo str_replace($arr2,$arr1,$str),"\n";
  40. echo str_replace($arr1,$arr2,$str),"\n";
  41. echo str_replace($arr1,"some replacement",$str),"\n";
  42. da( str_replace("","eh, nothing will happen",$arr1) );
  43. da( str_replace("ing","",$arr2) );
  44. //da( str_replace("remo",$arr1,$arr1) );
  45. da( str_replace($arr2,$arr1,$arr2) );
  46. da( str_replace($arr1,$arr2,$arr1) );
  47. da( str_replace($arr1,"some replacement",$arr2) );
  48. ?>