PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/utf8/stristr.php

https://github.com/avidenie/Yet-Another-PHP-Framework
PHP | 50 lines | 17 code | 6 blank | 27 comment | 5 complexity | 813694edb1f15f0e68b6d55e0317dc60 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Yet Another PHP Framework package.
  4. * (c) 2010 Adrian Videnie <{@link mailto:avidenie@gmail.com avidenie@gmail.com}>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. *
  9. * @package Framework
  10. * @subpackage I18n
  11. * @author Adrian Videnie <avidenie@gmail.com>
  12. * @copyright 2010 Adrian Videnie
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT license
  14. */
  15. /**
  16. * Multi-byte aware version of stristr native PHP function.
  17. *
  18. * Case-insenstive UTF-8 version of strstr. Returns all of input string
  19. * from the first occurrence of needle to the end.
  20. *
  21. * @see http://php.net/stristr
  22. * @author Harry Fuecks <hfuecks@gmail.com>
  23. *
  24. * @param string The input string.
  25. * @param string The needle.
  26. * @return mixed The matched substring if found, false either wise.
  27. */
  28. function _stristr($str, $search)
  29. {
  30. // If everything is ASCII, use the native PHP implementation.
  31. if (Utf8::isAscii($str) && Utf8::isAscii($search)) {
  32. return stristr($str, $search);
  33. }
  34. if ('' == $search) {
  35. return $str;
  36. }
  37. $str_lower = mb_strtolower($str);
  38. $search_lower = mb_strtolower($search);
  39. preg_match('/^(.*?)' . preg_quote($search, '/') . '/s', $str_lower, $matches);
  40. if (isset($matches[1])) {
  41. return substr($str, strlen($matches[1]));
  42. }
  43. return false;
  44. }