首頁 >php教程 >PHP源码 >截取字符串长度(不区分中英文字符长度)

截取字符串长度(不区分中英文字符长度)

PHP中文网
PHP中文网原創
2016-05-25 17:12:521067瀏覽

跳至

static function tran($string,$length=0,$str='',$etc='...'){
	$result = '';

$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');

for($i = 0, $j = 0; $i < strlen($string); $i++)
{
if($j >= $length)
{
for($x = 0, $y = 0; $x < strlen($etc); $x++)
{
if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, &#39;0&#39;, STR_PAD_LEFT), &#39;0&#39;))
{
$x += $number - 1;
$y++;
}
else
{
$y += 0.5;
}
}

$length -= $y;

break;
}

if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, &#39;0&#39;, STR_PAD_LEFT), &#39;0&#39;))
{
$i += $number - 1;
$j++;
}
else
{
$j += 0.5;
}
}

for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
{
if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, &#39;0&#39;, STR_PAD_LEFT), &#39;0&#39;))
{
if($length < 1.0)
{
break;
}

$result .= substr($string, $i, $number);

$length -= 1.0;

$i += $number - 1;
}
else
{
$result .= substr($string, $i, 1);

$length -= 0.5;
}
}

$result = htmlentities($result, ENT_QUOTES, &#39;utf-8&#39;);

if($i < strlen($string))
{
$result .= $etc;
}
	if(!empty($str)){
		return $result;
	}
	else{
		echo $result;
	}
}

}

                   

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn