strspn.php 927 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /**
  3. * UTF8::strspn
  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 _strspn($str, $mask, $offset = NULL, $length = NULL)
  12. {
  13. if ($str == '' OR $mask == '')
  14. return 0;
  15. if (UTF8::is_ascii($str) AND UTF8::is_ascii($mask))
  16. return ($offset === NULL) ? strspn($str, $mask) : (($length === NULL) ? strspn($str, $mask, $offset) : strspn($str, $mask, $offset, $length));
  17. if ($offset !== NULL OR $length !== NULL)
  18. {
  19. $str = UTF8::substr($str, $offset, $length);
  20. }
  21. // Escape these characters: - [ ] . : \ ^ /
  22. // The . and : are escaped to prevent possible warnings about POSIX regex elements
  23. $mask = preg_replace('#[-[\].:\\\\^/]#', '\\\\$0', $mask);
  24. preg_match('/^[^'.$mask.']+/u', $str, $matches);
  25. return isset($matches[0]) ? UTF8::strlen($matches[0]) : 0;
  26. }