public function cutStr($document)
{
$document = trim($document);
if (strlen($document) <= 0) {
return $document;
}
$search = array("'<script[^>]*?>.*?</script>'si",
"'<[\/\!]*?[^<>]*?>'si",
"'([\r\n])[\s]+'",
"'/\s(?=\s)/'",
"'/[nrt]/'",
"'/[\n\r\t]/'",
"'/s(?=s)/'",
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
);
$replace = array("", "", "\\1", "\"", "&", "<", ">", " ");
$String = @preg_replace($search, $replace, $document);
return $this->dealSubStr($String, 200, true);
}
public function dealSubStr($String, $Length, $Append = false)
{
if (strlen($String) <= $Length) {
return $String;
} else {
$I = 0;
while ($I < $Length) {
$StringTMP = substr($String, $I, 1);
if (ord($StringTMP) >= 224) {
$StringTMP = substr($String, $I, 3);
$I = $I + 3;
} elseif (ord($StringTMP) >= 192) {
$StringTMP = substr($String, $I, 2);
$I = $I + 2;
} else {
$I = $I + 1;
}
$StringLast[] = $StringTMP;
}
$StringLast = implode("", $StringLast);
if ($Append) {
$StringLast .= "...";
}
return $StringLast;
}
}
- 调用 cutStr(document)即可(document 为获取的html 文本)