О компании Менеджмент Переводы Программирование Робототехника Все проекты Контакты
Админка
пожалуйста подождите

Получение числа символов в строке



// returns the number of symbols in the UTF-8-formatted string
// on error (invalid UTF-8 formatted string) returns false
function utf8StrLen( $string )
{
$length = 0;

$i = 0;
while ( $i < strlen( $string ) )
{
$code = ord( $string[$i] );
if ( ( $code & 0x80 ) == 0 )
$i = 1;
else if ( ( $code & 0xe0 ) == 0xc0 )
$i = 2;
else if ( ( $code & 0xf0 ) == 0xe0 )
$i = 3;
else if ( ( $code & 0xf8 ) == 0xf0 )
$i = 4;
else return false;
$length ;
};

// done
return $length;
}

Получение символа с заданным смещением от начала строки



// returns the string with UTF-8 formatted character
// at the position, specified, or false on error (the position is outside the
// string boundaries or the string is not well-formatted UTF-8 string)
function utf8CharAt( $string, $position )
{
// first checking the position to be inside the string
if ( $position < 0 || $position >= utf8StrLen( $string ) )
return false;

$char = false;

$i = 0;
$curPosition = 0;
$stringLength = strlen( $string );
while ( $i < $stringLength )
{
$code = ord( $string[$i] );
// retreaving the character, if at the position, required
if ( $curPosition == $position )
{
if ( ( $code & 0x80 ) == 0 )
$char = $string[$i];
else if ( ( $code & 0xe0 ) == 0xc0 && $i 1 < $stringLength )
$char = $string[$i].$string[$i 1];
else if ( ( $code & 0xf0 ) == 0xe0 && $i 2 < $stringLength )
$char = $string[$i].$string[$i 1].$string[$i 2];
else if ( ( $code & 0xf8 ) == 0xf0 && $i 3 < $stringLength )
$char = $string[$i].$string[$i 1].$string[$i 2].$string[$i 3];
else
return false;
break;
};
// moving forward
if ( ( $code & 0x80 ) == 0 )
$i = 1;
else if ( ( $code & 0xe0 ) == 0xc0 )
$i = 2;
else if ( ( $code & 0xf0 ) == 0xe0 )
$i = 3;
else if ( ( $code & 0xf8 ) == 0xf0 )
$i = 4;
else return false;
$curPosition ;
};

// done
return $char;
}
 
 
 
Языки
Темы
Copyright © 1999 — 2023
Зетка Интерактив