首页 > 编程 > Regex > 正文

PHP html标签正则替换并可自定义正则规则

2020-03-16 21:13:19
字体:
来源:转载
供稿:网友
PHP有个去除HTML标签的函数strip_tags,不过对于某些特殊符号不好使,下面这个函数的功能非常强大,同时用户还可以根据自己的需要进行正则替换.
 
 
复制代码代码如下:

<?php 
function pregstring($str){ 
$strtemp = trim($str); 
$search = array( 
"|'|Uis", 
"|<script[^>].*?</script>|Uis", // 去掉 javascript 
"|/[字定义/].*/[/字定义/]|Uis", // 去掉缩略图 
"|<[///!].*?[^<>]*?>|Uis", // 去掉 HTML 标记 
"'>(quot|#34);'i", // 替换 HTML 实体 
"'>(amp|#38);'i", 
"|,|Uis", 
"|[/s]{2,}|is", 
"[>nbsp;]isu", 
"|[$]|Uis", 
); 
$replace = array( 
"`", 
"", 
"", 
"", 
"", 
"", 
"", 
" ", 
" ", 
" ", 
); 
$text = preg_replace($search, $replace, $strtemp); 
return $text; 

echo pregstring(字符串); //使用方法 
?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表