+
95
-

回答

先将html标签去除,然后在进行文字长度截取,超过的话就加省略号,示例代码如下:

<?php
function getplaintextintrofromhtml($html, $numchars) {
// Remove the HTML tags
$html = strip_tags($html);

// Convert HTML entities to single characters
$html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');

$html_len = mb_strlen($html, 'UTF-8');
// Make the string the desired number of characters
// Note that substr is not good as it counts by bytes and not characters
$html = mb_substr($html, 0, $numchars, 'UTF-8');

// Add an elipsis

if ($html_len > $numchars) {
$html .= "…";
}

return $html;

}
echo getplaintextintrofromhtml("<h2>标题</h2><p>内容,我们今天说一下如何使用php进行文章摘要截取</p>",10);


网友回复

我知道答案,我要回答