# 帝国CMS如何自定义函数过滤字符代码

*Published:* 2023-12-13
*Author:* 客服001

#### 温馨提醒

如果文章内容或图片资源失效，请留言反馈，我们会及时处理，谢谢

本文最后更新于2023年12月13日，已超过 180天没有更新



首先在 e/class/connect.php 文件中加入一个自定义[函数](https://www.xarjtc.com/tag/%e5%87%bd%e6%95%b0 "查看函数此标签更多文章") 比如 NoHTML() 这个自己喜欢随便设置：

```
//去除HTML标记 
functionNoHTML($string){ 
$string= preg_replace("'<script[^>]*?>.*?</script>'si", "", $string);//去掉javascript 
$string= preg_replace("'<[/!]*?[^<>]*?>'si", "", $string); //去掉HTML标记 
$string= preg_replace("'([rn])[s]+'", "", $string); //去掉空白字符 
$string= preg_replace("'&(quot|#34);'i", "", $string); //替换HTML实体 
$string= preg_replace("'&(amp|#38);'i", "", $string); 
$string= preg_replace("'&(lt|#60);'i", "", $string); 
$string= preg_replace("'&(gt|#62);'i", "", $string); 
$string= preg_replace("'&(nbsp|#160);'i", "", $string); 
//以下为屏蔽电话号码正则
$string= preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$string);//隐藏电话号码中间四位
$string= preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$string);//隐藏手机号码中间四位
//屏蔽电话号码正则结束
return$string; 
}
```

然后去列表内容模板修改代码即可：

```
$r[smalltext]=esub(NoHTML($r[smalltext]),200,'......'); 
$listtemp='<li><strong><a href="[!--titleurl--]">[!--title--]</a></strong><p>[!--smalltext--]</p></li>';
```