html中replace函数,公用函数库HtmlReplace过滤HTML函数问题

公用函数库common.func.php中函数HtmlReplace($str,$rptype=0)具有替换HTML标记的作用,但是

$str = '

我是合肥装饰网工程师';

$str = HtmlReplace($str,0);

echo $str;

输出结果:

我是合肥装饰网工程师

可以看出

仍然存在

读一下HtmlReplace($str,$rptype=0)函数:

// $rptype = 0 表示仅替换 html标记

// $rptype = 1 表示替换 html标记同时去除连续空白字符

// $rptype = 2 表示严格替换 html标记同时去除所有空白字符

// $rptype = -1 表示仅替换 html危险的标记

function HtmlReplace($str,$rptype=0)

{

$str = stripslashes($str);

if($rptype==0)

{

$str = htmlspecialchars($str);

}

else if($rptype==1)

{

$str = htmlspecialchars($str);

$str = str_replace(" ",' ',$str);

$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);

}

else if($rptype==2)

{

$str = htmlspecialchars($str);

$str = str_replace(" ",'',$str);

$str = ereg_replace("[\r\n\t ]",'',$str);

}

else

{

$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);

$str = eregi_replace('script','script',$str);

$str = eregi_replace("]*>",'',$str);

}

return addslashes($str);

}

查看资料发现htmlspecialchars的作用:

htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。

预定义的字符是:

& (和号) 成为 &

" (双引号) 成为 "

' (单引号) 成为 '

< (小于) 成为 <

> (大于) 成为 >

因此,我在这里将HtmlReplace($str,$rptype=0)改装为:

function HtmlReplace($str,$rptype=0)

{

$str = stripslashes($str);

if($rptype==0)

{

$str = htmlspecialchars($str);

}

else if($rptype==1)

{

$str = htmlspecialchars($str);

$str = str_replace(" ",' ',$str);

$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);

}

else if($rptype==2)

{

$str = strip_tags($str);

$str = str_replace(" ",'',$str);

$str = ereg_replace("[\r\n\t ]",'',$str);

$str = eregi_replace("]*>",'',$str);

}

else

{

$str = ereg_replace("[\r\n\t ]{1,}",' ',$str);

$str = eregi_replace('script','script',$str);

$str = eregi_replace("]*>",'',$str);

}

return addslashes($str);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值