博客列表页面需要显示一段摘要。但在截取摘要的时候,遇到了很多的问题。
1. 单纯的按照字数截取,会出现半截话的问题。
2. 在截取的时候,还要注意html标签的闭合问题。
可以解决的方案就是在添加文章的时候,添加一个特殊的分割符,比如drupal就是使用这样的方式来手工来决定何处作为截取。但这样不方便。
下面是我的EasyCMS的截取代码,与大家分享。
$FileContent = split("\n|<br>|<br />|</p>|</li>", $File['FileContent']);
$LineCount = count($FileContent);
for($I = 0; $I < $LineCount; $I ++)
{
if(!empty($File['Desc']))
{
$File['Desc'] .= $FileContent[$I] . "\n";
}
else
{
$File['Desc'] = $FileContent[$I] . "\n";
}
if(strlen($File['Desc']) > $_CFG['Blog']['DescLength']) break;
}
$File['Desc'] = nl2br(strip_tags(trim($File['Desc']), '<h3><a><strong><br /><img><ul><ol><li>'));
if(strpos($File['Desc'], '<strong>') !== false) $File['Desc'] .= '</strong>';
if(strpos($File['Desc'], '<h3>') !== false) $File['Desc'] .= '</h3>';
if(strpos($File['Desc'], '<ol>') !== false) $File['Desc'] .= '</ol>';
if(strpos($File['Desc'], '<ul>') !== false) $File['Desc'] .= '</ul>';
$File['Desc'] = stripslashes($File['Desc']);
