str_ireplace.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * UTF8::str_ireplace
  4. *
  5. * @package Kohana
  6. * @author Kohana Team
  7. * @copyright (c) Kohana Team
  8. * @copyright (c) 2005 Harry Fuecks
  9. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
  10. */
  11. function _str_ireplace($search, $replace, $str, & $count = NULL)
  12. {
  13. if (UTF8::is_ascii($search) AND UTF8::is_ascii($replace) AND UTF8::is_ascii($str))
  14. return str_ireplace($search, $replace, $str, $count);
  15. if (is_array($str))
  16. {
  17. foreach ($str as $key => $val)
  18. {
  19. $str[$key] = UTF8::str_ireplace($search, $replace, $val, $count);
  20. }
  21. return $str;
  22. }
  23. if (is_array($search))
  24. {
  25. $keys = array_keys($search);
  26. foreach ($keys as $k)
  27. {
  28. if (is_array($replace))
  29. {
  30. if (array_key_exists($k, $replace))
  31. {
  32. $str = UTF8::str_ireplace($search[$k], $replace[$k], $str, $count);
  33. }
  34. else
  35. {
  36. $str = UTF8::str_ireplace($search[$k], '', $str, $count);
  37. }
  38. }
  39. else
  40. {
  41. $str = UTF8::str_ireplace($search[$k], $replace, $str, $count);
  42. }
  43. }
  44. return $str;
  45. }
  46. $search = UTF8::strtolower($search);
  47. $str_lower = UTF8::strtolower($str);
  48. $total_matched_strlen = 0;
  49. $i = 0;
  50. while (preg_match('/(.*?)'.preg_quote($search, '/').'/s', $str_lower, $matches))
  51. {
  52. $matched_strlen = strlen($matches[0]);
  53. $str_lower = substr($str_lower, $matched_strlen);
  54. $offset = $total_matched_strlen + strlen($matches[1]) + ($i * (strlen($replace) - 1));
  55. $str = substr_replace($str, $replace, $offset, strlen($search));
  56. $total_matched_strlen += $matched_strlen;
  57. $i++;
  58. }
  59. $count += $i;
  60. return $str;
  61. }