How to replace sensitive words in php

藏色散人
Release: 2023-03-17 10:52:01
Original
1786 people have browsed it

php method to replace sensitive words: 1. Create a php sample file; 2. Define a form comment form; 3. Use the "str_replace($word, "***", $pinglun);" method Just replace sensitive words in comments with asterisks.

How to replace sensitive words in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to replace sensitive words in php?

The code is as follows:

<!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[&#39;pinglun&#39;];
$word=[&#39;傻逼&#39;,&#39;二百五&#39;,&#39;垃圾&#39;];
if (str_replace($word, "", $pinglun)!==$pinglun) {
echo "<script>alert(&#39;您的评论存在敏感词,将会被替换&#39;);</script>";
$str=str_replace($word, "***", $pinglun);
exit("<script>alert(&#39;您替换后的评论是:{$str}&#39;);</script>");
}else{
exit("<script>alert(&#39;您的评论是:{$pinglun}&#39;);</script>");
}
}

 ?>
Copy after login

Note: The str_replace() function replaces some characters in the string (case-sensitive).

This function must follow the following rules:

If the searched string is an array, then it will return an array. If the string being searched is an array, then it will find and replace each element in the array. If an array needs to be searched and replaced at the same time, and the elements to be replaced are less than the number of elements found, the excess elements will be replaced with empty strings. If you search an array and replace only one string, the replacement string will apply to all found values.

Note: This function is case-sensitive. Please use the str_ireplace() function to perform a case-insensitive search.

Note: This function is binary safe.

Grammar

str_replace(find,replace,string,count)
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to replace sensitive words in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template