How to block keywords using regular expressions
Filter keywords. When we submit data, we usually use trim to filter the spaces before and after. In fact, when we are familiar with regular expressions, we completely use regular expressions to filter out the spaces entered by the user, and we can use this to filter out any spaces. The space in the position; to expand, this may be the reason why the name cannot contain spaces. Of course, the real reason is due to the limitation of language string naming and not that spaces are filtered out. The idea used here is to remember yourself across domains. The knowledge learned is conducive to memory and learning; here it only represents personal opinions at this moment.
Keyword blocking is a must-have function for social software. When we are familiar with regular expressions, we completely use regular expressions to filter out the spaces entered by the user, and we can use regular expressions to filter out spaces in any position. Specific regular expressions You can refer to this article for how to block keywords.
[Question]Keyword blocking is a must-have function for social software. Of course, it is generally done in the middle and later stages of the product; different products have different regulations, and it depends on the product. It’s okay to go through operations
[Method]We look at this problem from a technical perspective. To achieve a function, the latter means to achieve a requirement. The methods are diverse, and the key points are It is to find the one that is suitable for our current products; for example: we can process data on the back end and then pass it to the front end; we can also process data on the front end; what we are talking about here is the JS processing method on the front end and the PHP processing method on the back end
【JS method】
// 进行屏蔽的文字 var str = '小丽、小明和小红是校长的学生;在这个小家庭里面,校长就是我们的老师!'; // 关键字组,这个可以在前端进行定义,也可以来自后台 var arr = ['小丽','小明','小红','家庭','校长']; var res = str.replace(new RegExp(arr.join('|'),'img'),'*'); console.log(res); // *、*和*是*的学生;在这个小*里面,*就是我们的老师!
【PHP method】
// 进行屏蔽的文字 $str = '小丽、小明和小红是校长的学生;在这个小家庭里面,校长就是我们的老师!'; // 关键字组 $arr = ['小丽','小明','小红','家庭','校长']; echo preg_replace('/'.join($arr,'|').'/','*',$str); // *、*和*是*的学生;在这个小*里面,*就是我们的老师!
The difference is the method, the same is the idea;
The above content is how to use regular expressions to block keywords. I hope it will be helpful to everyone.
Related recommendations:
How to use regular expressions to block keywords
The most complete PHP regular expression in history_regular expression
Detailed explanation of commonly used functions in php regular expressions
The above is the detailed content of How to block keywords using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Golang regular expressions use the pipe character | to match multiple words or strings, separating each option as a logical OR expression. For example: matches "fox" or "dog": fox|dog matches "quick", "brown" or "lazy": (quick|brown|lazy) matches "Go", "Python" or "Java": Go|Python |Java matches words or 4-digit zip codes: ([a-zA

PHP regular expressions are a powerful tool for text processing and conversion. It can effectively manage text information by parsing text content and replacing or intercepting it according to specific patterns. Among them, a common application of regular expressions is to replace strings starting with specific characters. We will explain this as follows

In-depth analysis of the role and usage of the static keyword in C language. In C language, static is a very important keyword, which can be used in the definition of functions, variables and data types. Using the static keyword can change the link attributes, scope and life cycle of the object. Let’s analyze the role and usage of the static keyword in C language in detail. Static variables and functions: Variables defined using the static keyword inside a function are called static variables, which have a global life cycle

How to remove Chinese in PHP using regular expressions: 1. Create a PHP sample file; 2. Define a string containing Chinese and English; 3. Use "preg_replace('/([\x80-\xff]*)/i', '',$a);" The regular method can remove Chinese characters from the query results.

In this article, we will learn how to remove HTML tags and extract plain text content from HTML strings using PHP regular expressions. To demonstrate how to remove HTML tags, let's first define a string containing HTML tags.

Title: Is go a keyword in C language? Detailed analysis In C language, "go" is not a keyword. Keywords in C language are specified by the C standard and are used to represent specific grammatical structures or functions. They have special meanings in the compiler and cannot be used as identifiers or variable names. For example, the keyword "int" represents an integer data type, "if" represents a conditional statement, and so on. If we want to verify whether "go" is a keyword in C language, we can write a simple program to test it. Here is an example: #inc

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

Website security has attracted more and more attention, and using the HTTPS protocol to ensure the security of data transmission has become an important part of current website development. In PHP development, how to use regular expressions to verify whether the URL is HTTPS protocol? here we come to find out. Regular expression Regular expression is an expression used to describe rules. It is a powerful tool for processing text and is widely used in text matching, search and replacement. In PHP development, we can use regular expressions to match http in the URL
