str_ireplace.php 1.6 KB

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