Methods for masking keywords using regular expressions

Source: Internet
Author: User
Tags ming
Filter keywords, we usually carry out data submission will be used to filter before and after the white space, in fact, when we are familiar with the regular, we completely use the regular to filter out the user input space, and with this can be filtered out of any position of the space; in the extension maybe that's why the name can't contain spaces. , of course, the real reason is because the language string naming restrictions and not the space is filtered out, the idea here is that the cross-domain memory of their own learning knowledge, conducive to memory and learning;

Keyword blocking is the social software must do the function, when we are familiar with the regular, we completely use the regular to filter out the user input space, and with regular can filter out any space, the specific method of shielding the keyword can be referred to this article.

"problem" keyword blocking is the social software must do the function, of course, is generally the product of the middle and late to do; different product specifications, follow the product operation, you can

"Method" we look at this problem from a technical point of view, to achieve a function the latter said that the implementation of a demand, its methods are various, the focus is to find suitable for our current products; What we're going to say here is the front-end JS processing method, the back-end PHP processing method

"JS Method"

Text to block var str = ' Xiao Li, Xiao Ming and Xiao Hong are the principals ' students; in this small family, the headmaster is our teacher! ';//keyword group, this can be defined at the front end, can also come from the background var arr = [' Xiao Li ', ' Xiao Ming ', ' Little Red ', ' family ', ' headmaster '];var res = str.replace (New RegExp (Arr.join (' | '), ' img ') ), ' * ', Console.log (res),//*, * and * are students; in this small *, * is our teacher!

"Php Method"

Block text $str = ' Xiao Li, Xiao Ming and Xiao Hong are the principals ' students; in this small family, the headmaster is our teacher! ';//keyword Group $arr = [' Xiao Li ', ' Xiao Ming ', ' Little Red ', ' family ', ' Headmaster '];echo preg_replace ('/'. Join ($arr, ' | '). ' /', ' * ', $str);//*, * and * are students; in this small * inside, * is our teacher!

The difference is the method, the same is the idea;

The above is the use of regular expressions to block the keyword method, we hope to help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.