Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions to mask keywords

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to use regular expressions to mask keywords". The content is easy to understand and clear, hoping to help you solve your doubts. Let me lead you to study and learn this article "how to use regular expressions to mask keywords".

[problem] keyword masking is a necessary function of social software, of course, generally speaking, it is done in the middle and later stages of the product; different product regulations are different, so it is OK to follow the product operation.

[methods] We see this problem from a technical point of view. To implement a function, the latter says that there are a variety of ways to achieve a requirement, with the focus on finding something suitable for our current products; for example, we can process the data at the back end and pass it to the front end; we can also process the data at the front end; here we are talking about the front end JS processing method, the back end php processing method

[JS method]

/ / the blocked text var str = 'Xiao Li, Xiao Ming and Xiao Hong are the headmaster's students; in this small family, the headmaster is our teacher!' / / keyword group, which can be defined at the front end or from the background var arr = ['Xiao Li', 'Xiao Ming', 'Xiao Hong', 'family', 'headmaster']; var res = str.replace (new RegExp (arr.join ('|), 'img'),' *'); console.log (res); / / *, * and * are * students; in this Xiao *, * is our teacher!

[PHP method]

/ / blocked text $str = 'Xiao Li, Xiao Ming and Xiao Hong are the headmaster's students; in this small family, the headmaster is our teacher!' / / keyword group $arr = ['Xiao Li', 'Xiao Ming', 'Xiao Hong', 'family', 'headmaster']; echo preg_replace ('/ '.join ($arr,' |'). / / *, $str); / / *, * and * are * students; in this Xiao *, * is our teacher! The above is all the content of the article "how to use regular expressions to block keywords". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report