字符截取+删除html

function cutdot($Str, $Length, $dot) {//$Str为截取字符串,$Length为需要截取的长度
global $s;
$i = 0;
$l = 0;
$ll = strlen($Str);
$s = $Str;
$f = true;

while ($i <= $ll) {
if (ord($Str{$i}) < 0x80) {
$l++; $i++;
} else if (ord($Str{$i}) < 0xe0) {
$l++; $i += 2;
} else if (ord($Str{$i}) < 0xf0) {
$l += 2; $i += 3;
} else if (ord($Str{$i}) < 0xf8) {
$l += 1; $i += 4;
} else if (ord($Str{$i}) < 0xfc) {
$l += 1; $i += 5;
} else if (ord($Str{$i}) < 0xfe) {
$l += 1; $i += 6;
}

if (($l >= $Length) && $f) {
$s = substr($Str, 0, $i);
$f = false;
}

if (($l > $Length) && ($i < $ll)) {
$s = $s;
return $s.$dot; //如果当前串长度小与指定长度,则退出。
}
}
return $s;
}

//删除html样式,得到纯文本
function deleteHtmlTags($string, $br = false,$Length=””,$dot=””)
{
while(strstr($string, ‘>’))
{
$currentBeg = strpos($string, ‘<‘);
$currentEnd = strpos($string, ‘>’);
$tmpStringBeg = @substr($string, 0, $currentBeg);
$tmpStringEnd = @substr($string, $currentEnd + 1, strlen($string));
$string = $tmpStringBeg.$tmpStringEnd;
}

if(!empty($Length)){
return cutdot($string, $Length, $dot);
}else{
return $string;
}
}


关注公众号,了解更多it技术(it问答网

发表评论

电子邮件地址不会被公开。