Php blocking Keyword implementation method

Source: Internet
Author: User
This article describes how to implement the PHP blocking keyword. There are two ways to implement the first method.

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

$ Str = "/Hello | goodbye | what | DY/"; // keyword regular string $ string = "what have you done? "; // Text string echo preg_replace ($ str," * ", $ string); // preg_replace () executes a regular expression to match and replace

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; // return false if no matching keyword exists} $ content = "I met a SB today"; $ key = strPosFuck ($ content); if ($ key) {echo "keyword ". $ key; # code ...} else {echo "OK ";}

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.

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.