How to use regular expressions to block keywords
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 spaces entered by users, and regular expressions can filter out spaces at any position. Specifically, regular expressions block keywords. You can refer to this article for the method
[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 Product regulations are different. You can follow product operations.
[Method] We look at this problem from a technical perspective. To achieve a function, the latter means to achieve a requirement. The method There are many kinds, and the key point is to find the one that suits our current products; for example: we can process the data on the back end and then pass it to the front end; we can also process the data on the front end; what we are going to talk about here is the front-end JS processing method , back-end php processing method
[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); // *、*和*是*的学生;在这个小*里面,*就是我们的老师!
[Summary]The difference is the method, the same is the idea;
[Extension]We are not only It is only used to 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 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 expresses personal opinions at this moment
The above is the detailed content of How to use regular expressions to block keywords. 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

AI Hentai Generator
Generate AI Hentai for free.

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



With the popularity of smart phones, we receive a large number of text messages every day, some of which are advertising and promotional messages, and some of which are spam text messages. These text messages not only waste our time, but also occupy the space of our mobile phones. Fortunately, however, iPhones offer some features to block these annoying text messages. This article will introduce how to block text messages using iPhone. To block text messages, first open the Settings app, then scroll and tap Messages. In the information settings interface, you can see some options, including "Blocked

We need to use WeChat APP all the time every day. The functions here are rich and diverse, not only for everyone to chat here, but also for you to handle various things in life. They can solve some problems very well, and their lives can become better and better. Nowadays, everyone still likes to check the circle of friends. You will find that there are some life updates posted by your friends on the circle of friends. information, record some of your wonderful life, and set the visibility range of your Moments. You can view these Moments within a specified time. It is your own decision. Every time you check your Moments, you will be able to discover something. Everyone is very irritated by the advertising push interface and wants to close it.

We all particularly like the Tantan social platform. It is very safe and reliable. We can all make a lot of friends on the Internet here. If we match randomly, we will definitely be able to match users who like each other. When everyone is successfully matched, they can safely chat socially here. Chatting every day can bring everyone closer and become closer, and many friends are using this social network. When you are on the platform, you definitely don’t want some of your friends or some friends in your mobile address book to find out. To avoid some embarrassing situations, you can use the method of blocking contacts here, and there are not many If you know how to block contacts, you can read these tutorials brought to you by the editor.

When opening some software or browsers on a win7 computer, many netizens may encounter advertising pop-ups. Sometimes they will appear even after they are closed, which affects their mood. So how to block advertising pop-ups in win7? The editor below will teach you how to block ad pop-ups in Windows 7. The specific steps are as follows: 1. Click the ‘Start’ button on the lower left side of the computer and click ‘Control Panel’ to open it. 2. Find ‘Internet Options’ and open it. 3. Click ‘Privacy’. 4. Find the ‘Settings’ button below, as shown in the picture. 5. Click on ‘Block Level’. 6. Generally, it is set to the intermediate organizational level. You can also set it to the advanced level. The advanced level means that all pop-up windows are prohibited, as shown in the figure. The above is how to block ad pop-ups in win7 system

1. How can Zhaopin Recruitment block the company from viewing my resume? Tutorial on Zhaopin Recruitment to block companies from seeing your resume! 1. Open the downloaded Zhaopin Recruitment app, log in to your account, and enter the main page. 2. After entering the main page, click My and select Online Resume. 3. After reaching the resume interface, click on the upper right corner and open the privacy settings option. 4. After entering the privacy interface, click Block Company. 5. In the interface to add blocking, enter the name of the company you want to block. 6. After final selection, click the block button below. It has been set up and the company will not be able to view your resume.

As one of the most popular short video platforms in the world, Douyin provides a stage for users to show themselves and share their lives. During use, users may encounter situations where they are blocked by the other party. So, what is the status after Douyin is blocked by the other party? Can I still search for him? This article will answer these questions for you. 1. What is the status after Douyin is blocked by the other party? When your Douyin account is blocked by the other party, you will not be able to view the other party's works, comments, likes, etc. on the Douyin platform. The other party also cannot view your works, comments, likes, etc. When both parties block each other, each other will completely disappear on the Douyin platform. 2. Can I still search for him after his TikTok is blocked? When your Douyin account is blocked by the other party, you enter the other party’s nickname in the Douyin search bar.

1. First open the Weibo group chat and select Settings. 2. Then select group message reminder. 3. Finally select [Do Not Disturb Messages].

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
