Finds the length of the initial segment not matching mask. This is a UTF8-aware version of strcspn.
$found = UTF8::strcspn($str, $mask);
integer Core_UTF8::strcspn( string $str , string $mask [, integer $offset = null , integer $length = null ] )
参数列表
参数 类型 描述 默认值 $str
string
Input string $mask
string
Mask for search $offset
integer
Start position of the string to examine null $length
integer
Length of the string to examine null
integer
length of the initial segment that contains characters not in the maskpublic static function strcspn($str, $mask, $offset = null, $length = null)
{
if ( $str == '' || $mask == '' ) return 0;
if ( UTF8::is_ascii($str) && UTF8::is_ascii($mask) ) return ($offset === null) ? strcspn($str, $mask) : (($length === null) ? strcspn($str, $mask, $offset) : strcspn($str, $mask, $offset, $length));
if ( $offset !== null || $length !== null )
{
$str = UTF8::substr($str, $offset, $length);
}
// Escape these characters: - [ ] . : \ ^ /
// The . and : are escaped to prevent possible warnings about POSIX regex elements
$mask = preg_replace('#[-[\].:\\\\^/]#', '\\\\$0', $mask);
preg_match('/^[^' . $mask . ']+/u', $str, $matches);
return isset($matches[0]) ? UTF8::strlen($matches[0]) : 0;
}