PageRenderTime 77ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Smarty/plugins/shared.mb_str_replace.php

https://bitbucket.org/miguelgazela/rogo
PHP | 55 lines | 36 code | 2 blank | 17 comment | 8 complexity | 00ad04ec798345c9da214d4eae1c8ba5 MD5 | raw file
  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. ?>