PHP blocking keyword implementation method, php blocking keyword

Source: Internet
Author: User

PHP blocking keyword implementation method, php blocking keyword

This article describes how to implement the PHP blocking keyword. There are two implementation methods:

Method 1

Use regular expressions to match keywords and replace them with other characters.

$ Str = "/ | you are paralyzed | what things | SB | fuck/"; // keyword regular string $ string = "What the fuck are you doing SB "; // text string echo preg_replace ($ str, "*", $ string); // preg_replace () executes matching and replacement of a regular expression

The "OK" method is quite simple. When you are reading it, You can first think about it based on your own ideas.

Method 2

Ideas

1. Put keywords in a txt document and use the file_get_contents () function to read keywords from the document.

2. Use the function explode () to split the string into an array loop array strpos () to find matching keywords.

Code

Header ('content-type: textml; charset = UTF-8; '); function strPosFuck ($ content) {$ fuck = file_get_contents('keyWords.txt '); // read the keyword text information $ content = trim ($ content); $ fuckArr = explode ("\ n", $ fuck ); // convert the keyword to an array for ($ I = 0; $ I <count ($ fuckArr); $ I ++) {// $ fuckArr [$ I] = trim ($ fuckArr [$ I]); if ($ fuckArr [$ I] = "") {continue; // skip this loop if the keyword is null # code ...} if (strpos ($ content, trim ($ fuckArr [$ I])! = False) {return $ fuckArr [$ I]; // If a keyword is matched, the keyword # code...} is returned ...}} return false; // if no matching keyword exists, return false} $ content = "I met a SB today"; $ key = strPosFuck ($ content ); if ($ key) {echo "keyword ". $ key; # code ...} else {echo "OK ";}

NotesBe sure to leave it empty, be sure to leave it empty,

When the value returned by the strops () function is either false or the keyword location is determined, pay attention

After success, you can think about how to make up a string or array by returning all the matched keywords.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.