modifier.replace.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Smarty plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsModifier
  7. */
  8. /**
  9. * Smarty replace modifier plugin
  10. *
  11. * Type: modifier<br>
  12. * Name: replace<br>
  13. * Purpose: simple search/replace
  14. *
  15. * @link http://smarty.php.net/manual/en/language.modifier.replace.php replace (Smarty online manual)
  16. * @author Monte Ohrt <monte at ohrt dot com>
  17. * @author Uwe Tews
  18. * @param string $
  19. * @param string $
  20. * @param string $
  21. * @return string
  22. */
  23. function smarty_modifier_replace($string, $search, $replace)
  24. {
  25. if (!function_exists('mb_str_replace')) {
  26. // simulate the missing PHP mb_str_replace function
  27. function mb_str_replace($needles, $replacements, $haystack)
  28. {
  29. $rep = (array)$replacements;
  30. foreach ((array)$needles as $key => $needle) {
  31. $replacement = $rep[$key];
  32. $needle_len = mb_strlen($needle);
  33. $replacement_len = mb_strlen($replacement);
  34. $pos = mb_strpos($haystack, $needle, 0);
  35. while ($pos !== false) {
  36. $haystack = mb_substr($haystack, 0, $pos) . $replacement
  37. . mb_substr($haystack, $pos + $needle_len);
  38. $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
  39. }
  40. }
  41. return $haystack;
  42. }
  43. }
  44. if (function_exists('mb_substr')) {
  45. return mb_str_replace($search, $replace, $string);
  46. } else {
  47. return str_replace($search, $replace, $string);
  48. }
  49. }
  50. ?>