为了账号安全,请及时绑定邮箱和手机立即绑定

帮忙分析如下代码中,关于 str_replace() 函数的替换问题!

帮忙分析如下代码中,关于 str_replace() 函数的替换问题!

HUWWW 2022-02-25 19:15:04
下面这个在 文件名为 function.php里<?phpfunction unhtml($content){$content=str_replace("&","& a m p;",$content);$content=str_replace("<","& l t;",$content);$content=str_replace(">","& g t;",$content);$content=str_replace(" ","&n b s p;",$content);$content=str_replace(chr(13),"<br>",$content);$content=str_replace("\\","\\\\",$content);$content=str_replace("台什么","不文明用语",$content);$content=str_replace(chr(34),""",$content);$content=str_replace("[emt]","<img src=images/xyq/",$content);$content=str_replace("[/emt]",".gif/>",$content);return $content;}?>下面在这个SHOW.php里从数据库里读取1段文字(值给$content)导入函数<?php include_once("function.php");echo unhtml($content);?>结果是 该屏蔽的没屏蔽 该替换的没换比如台什么[emt]1[/emt]换成不文明用语<img src=images/xyq/1.gif/>才对嘛
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

程序里面下面这一句有语法错误:
$content=str_replace(chr(34),""",$content);

可以修改为:
$content=str_replace(chr(34),'"'",$content);

或者取消,好像没有什么本质意义。

程序前面几行替换HTML代码的,代码中间不能有空格,你写错了。

我修改后,完整的测试程序如下,测试通过,没有问题,能够替换:

<?php
function unhtml($content)
{
$content=str_replace("&","&amp;",$content);
$content=str_replace("<","&lt;",$content);
$content=str_replace(">","&gt;",$content);
$content=str_replace(" ","&nbsp;",$content);
$content=str_replace(chr(13),"<br>",$content);
$content=str_replace("\\","\\\\",$content);
$content=str_replace("台什么","不文明用语",$content);
$content=str_replace("[emt]","<img src=images/xyq/",$content);
$content=str_replace("[/emt]",".gif/>",$content);
return $content;
}

$str='台什么[emt]1[/emt]';
echo unhtml($str);
?>

程序运行过程如下:

E:\ygb>php a.php
不文明用语<img src=images/xyq/1.gif/>

 


查看完整回答
反对 回复 2022-02-28
  • 1 回答
  • 0 关注
  • 160 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号