最佳答案
对 HTML 进行截断:
closetags(str_limit($article->body, 1000))
闭合标签:
function closetags($html) {
libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $html);
// Strip wrapping <html> and <body> tags
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
讨论数量:
对 HTML 进行截断:
closetags(str_limit($article->body, 1000))
闭合标签:
function closetags($html) {
libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $html);
// Strip wrapping <html> and <body> tags
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
对 HTML 进行截断:
closetags(str_limit($article->body, 1000))
闭合标签:
function closetags($html) {
libxml_use_internal_errors(true);
$dom = new \DOMDocument;
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">' . $html);
// Strip wrapping <html> and <body> tags
$mock = new \DOMDocument;
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$mock->appendChild($mock->importNode($child, true));
}
return trim($mock->saveHTML());
}
推荐文章: