fork download
  1. <?php
  2.  
  3. // your code goes here
  4. $a[0] = "tomek";
  5. $a[1] = "tomi";
  6. $tekst = "jan % b% dobry";
  7.  
  8. $newstr = str_replace_array("%", $a, $tekst);
  9.  
  10. echo "'".$newstr."'";
  11.  
  12. function str_replace_array($from, $to, $subject) {
  13. $result = $subject;
  14.  
  15. for($i = 0, $cnt = count($to); $i < $cnt; $i++) {
  16. $result = str_replace_first($from, $to[$i], $result);
  17. }
  18.  
  19. return $result;
  20. }
  21.  
  22. function str_replace_first($from, $to, $subject) {
  23.  
  24. $pos = strpos($subject, $from);
  25. if ($pos !== false) {
  26. $result = substr_replace($subject, $to, $pos, strlen($from));
  27. } else {
  28. $result = $subject;
  29. }
  30.  
  31. return $result;
  32. }
  33.  
  34.  
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
'jan tomek był tomi dobry'