帝国CMS内容页面过滤清理简介smalltext里前后空格的方法

想必各位站长在做站的过程中,特别是采集的时候,难免在简介字段smalltext里会出现一些空格,有的在前,有的在后,但是如果在内容页的TDK里填写了[!--pagedes--]的话,空格也会显示出来。那么按照下面的方法便可以消除代码里的空格,对搜索引擎更好。

第一种写法,直接写在模版代码里:

<?
$qian=array(" "," ","\t","\n","\r");$hou=array("","","","","");
$smalltext=str_replace($qian,$hou,$navinfor[smalltext]);
echo $smalltext;
?>

第二种写法,需要修改相关函数:

我们构造一个过滤函数,将其放在“/e/class/userfun.php”里面,代码如下:

function phome_format_html($str){
$str=trim($str);
$str=str_replace('&','',$str);
$str=str_replace('ldquo;','“',$str);
$str=str_replace('rdquo;','”',$str);
$str=str_replace('middot;','·',$str);
$str=str_replace('lsquo;','‘',$str);
$str=str_replace('rsquo;','’',$str);
$str=str_replace('hellip;','…',$str);
$str=str_replace('mdash;','—',$str);
$str=str_replace('ensp;','',$str);
$str=str_replace('emsp;','',$str);
$str=str_replace('nbsp;','',$str);
$str=str_replace(' ','',$str);
$str=str_replace('t','',$str);
$str=str_replace('rn','',$str);
$str=str_replace('r','',$str);
$str=str_replace('n','',$str);
$str=str_replace(' ','',$str);
$str = preg_replace('/s(?=s)/','', $str);// 接着去掉两个空格以上的
$str = preg_replace('/[nrt]/',' ', $str);// 最后将非空格替换为一个空格
return trim($str);
}

接下来我们在前端模板中调用该函数,使用方式如下,就可以实现过滤帝国cms模板中输出数据的html字符了。

内容页:

<?=phome_format_html($navinfo['smalltext'])?>

列表页

'.phome_format_html($r[smalltext]).'

在内容页模版代码里你需要调用smalltext简介字段的地方,使用上面的代码即可输出过滤前后空格的简介。

给TA打赏
共{{data.count}}人
人已打赏
帝国CMS教程

帝国CMS后台信息列表标题显示不全的问题和解决方法

2026-3-11 15:12:57

帝国CMS教程

帝国cms 内容页简介标签smalltext控制字数的方法

2026-3-11 15:14:57

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索