您的位置:易软博客 > 技术笔记 > EasyCMS的首页摘要代码

EasyCMS的首页摘要代码

添加日期:2007-08-29 21:11 作者:chunsheng.wang 来源:本站原创 点击:1040

博客列表页面需要显示一段摘要。但在截取摘要的时候,遇到了很多的问题。

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']);
责任编辑:wwccss 于 2008-10-03 15:16
« win2000下面安装apache2.2+php5.2+mysql 技术笔记 在AS4下面安装apache2.2 + php 5.2.4 »
评论:
#1 王春生 at 2007-08-30 12:33
嗯。是不太严谨了。呵呵。我考虑是不是可以使用tidy来自动修正html代码。自己的东西,怎么样都好办。:)
#2 xLight at 2007-08-30 09:23
这样的话,$File['Desc']中应该可能会存在一些</ul></ol></strong></h3>

而只判断有<ul><ol><strong><h3>就闭合,会不会不太严谨?
#3 xLight at 2007-08-30 09:15
根据手册上strip_tags的Example:

Example 1. strip_tags() example

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> Other text';
echo strip_tags($text);
echo "\n";

// Allow <p>
echo strip_tags($text, '<p>');
?>

The above example will output:

Test paragraph. Other text
<p>Test paragraph.</p> Other text

称呼: 邮箱:
评论:
流量统计