php怎么替换敏感词
php替换敏感词的方法:1、创建一个php示例文件;2、定义一个form评论表单;3、通过“str_replace($word, "***", $pinglun);”方式将评论里的敏感词替换成星号即可。
php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用
本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。
php怎么替换敏感词?
代码如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>敏感词替换</title> </head> <body> <form action="" method="post"> <label for="">请输入您的评论:</label><br> <textarea name="pinglun" id="" cols="30" rows="10"></textarea><br> <input type="submit" value="提交"> </form> </body> </html> <?php if ($_POST) { $pinglun=$_POST['pinglun']; $word=['傻逼','二百五','垃圾']; if (str_replace($word, "", $pinglun)!==$pinglun) { echo "<script>alert('您的评论存在敏感词,将会被替换');</script>"; $str=str_replace($word, "***", $pinglun); exit("<script>alert('您替换后的评论是:{$str}');</script>"); }else{ exit("<script>alert('您的评论是:{$pinglun}');</script>"); } } ?>
登录后复制