shared.mb_str_replace.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Smarty shared plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsShared
  7. */
  8. if (!function_exists('smarty_mb_str_replace')) {
  9. /**
  10. * Multibyte string replace
  11. *
  12. * @param string $search the string to be searched
  13. * @param string $replace the replacement string
  14. * @param string $subject the source string
  15. * @param int &$count number of matches found
  16. * @return string replaced string
  17. * @author Rodney Rehm
  18. */
  19. function smarty_mb_str_replace($search, $replace, $subject, &$count=0)
  20. {
  21. if (!is_array($search) && is_array($replace)) {
  22. return false;
  23. }
  24. if (is_array($subject)) {
  25. // call mb_replace for each single string in $subject
  26. foreach ($subject as &$string) {
  27. $string = &smarty_mb_str_replace($search, $replace, $string, $c);
  28. $count += $c;
  29. }
  30. } elseif (is_array($search)) {
  31. if (!is_array($replace)) {
  32. foreach ($search as &$string) {
  33. $subject = smarty_mb_str_replace($string, $replace, $subject, $c);
  34. $count += $c;
  35. }
  36. } else {
  37. $n = max(count($search), count($replace));
  38. while ($n--) {
  39. $subject = smarty_mb_str_replace(current($search), current($replace), $subject, $c);
  40. $count += $c;
  41. next($search);
  42. next($replace);
  43. }
  44. }
  45. } else {
  46. $parts = mb_split(preg_quote($search), $subject);
  47. $count = count($parts) - 1;
  48. $subject = implode($replace, $parts);
  49. }
  50. return $subject;
  51. }
  52. }
  53. ?>