function strcut_utf8($str, $len, $checkmb=false, $tail='')
{
/**
* UTF-8 Format
* 0xxxxxxx = ASCII, 110xxxxx 10xxxxxx or 1110xxxx 10xxxxxx 10xxxxxx
* latin, greek, cyrillic, coptic, armenian, hebrew, arab characters consist of 2bytes
* BMP(Basic Mulitilingual Plane) including Hangul, Japanese consist of 3bytes
**/
$html_decimalcodes = array("​","◀");
preg_match_all('/[\xE0-\xFF][\x80-\xFF]{2}|./', $str, $match); // target for BMP
$m = $match[0];
$slen = strlen($str); // length of source string
$tlen = strlen($tail); // length of tail string
$mlen = count($m); // length of matched characters
if ($slen <= $len)
{
/*echo $str;
echo '
';*/
$str = str_replace($html_decimalcodes,"", $str);
/*echo $str;
echo '
';*/
return $str;
}
if (!$checkmb && $mlen <= $len)
{
/*echo $str;
echo '
';*/
$str = str_replace($html_decimalcodes,"", $str);
/*echo $str;
echo '
';*/
return $str;
}
$ret = array();
$count = 0;
for($i=0; $i < $len; $i++) {
$count += ($checkmb && strlen($m[$i]) > 1)?2:1;
if($count + $tlen > $len) break;
$ret[] = $m[$i];
}
$text = html_entity_decode(join('', $ret).$tail);
/*echo $text;
echo '
';*/
$text = str_replace($html_decimalcodes,"", $text);
/*echo $text;
echo '
';*/
return $text;
}
{
/**
* UTF-8 Format
* 0xxxxxxx = ASCII, 110xxxxx 10xxxxxx or 1110xxxx 10xxxxxx 10xxxxxx
* latin, greek, cyrillic, coptic, armenian, hebrew, arab characters consist of 2bytes
* BMP(Basic Mulitilingual Plane) including Hangul, Japanese consist of 3bytes
**/
$html_decimalcodes = array("​","◀");
preg_match_all('/[\xE0-\xFF][\x80-\xFF]{2}|./', $str, $match); // target for BMP
$m = $match[0];
$slen = strlen($str); // length of source string
$tlen = strlen($tail); // length of tail string
$mlen = count($m); // length of matched characters
if ($slen <= $len)
{
/*echo $str;
echo '
';*/
$str = str_replace($html_decimalcodes,"", $str);
/*echo $str;
echo '
';*/
return $str;
}
if (!$checkmb && $mlen <= $len)
{
/*echo $str;
echo '
';*/
$str = str_replace($html_decimalcodes,"", $str);
/*echo $str;
echo '
';*/
return $str;
}
$ret = array();
$count = 0;
for($i=0; $i < $len; $i++) {
$count += ($checkmb && strlen($m[$i]) > 1)?2:1;
if($count + $tlen > $len) break;
$ret[] = $m[$i];
}
$text = html_entity_decode(join('', $ret).$tail);
/*echo $text;
echo '
';*/
$text = str_replace($html_decimalcodes,"", $text);
/*echo $text;
echo '
';*/
return $text;
}
No comments:
Post a Comment