PHP Block Keyword Method code example

Source: Internet
Author: User
Now the network information supervision is very strict, especially the blocking keyword. Especially now WEB2.0 era, the content of the website is almost all from the Netizen publishes, the stationmaster manages can. If you want someone to prohibit publishing a keyword on your site, you need to do it beforehand. Use PHP to do keyword masking function style has a variety of, such as regular is the most common one, here is not one by one examples, this article introduces two methods to implement the shielding keywords

The first of these methods

The idea uses the regular to match the keyword, replaces the keyword with another character

$STR = "/Your Uncle | You're paralyzed | What the hell | Sb| your mother/";  Keyword Regular string   $string = "What the hell are you doing, SB";    Text string  echo preg_replace ($str, "*", $string);    Preg_replace () performs a match and replace of a regular expression

OK method At the end is very simple to look at the time you can according to the thinking of their own first to think

Method Two

Ideas

1. Put the keyword into a txt document to be separated by a certain compliance, using the file_get_contents () function to read the keyword document into

2. Use the function explode () to split the string into an array of cyclic arrays Strpos () Find matching keywords

Code

Header (' content-type:textml; charset=utf-8; '); function Strposfuck ($content) {   $fuck = file_get_contents (' keyWords.txt ');  Read 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;  If the keyword is empty, skip this loop   # code ...   }    if (Strpos ($content, Trim ($FUCKARR [$i]))! = False)      {      return $fuckArr [$i];  If the keyword is matched, return the keyword       # code ...       }     }    return false;  Returns False if no match is reached to the   keyword   $content = "I have a SB for you today";    $key = Strposfuck ($content);   if ($key)   {    echo "presence keyword". $key;    # code ...    }   else    {     echo "OK";   }

pay attention to the point must go empty must go to empty,

Strops () The return value of the function is either false or the position of the keyword is judged

After success, you can think about how to return all matching keywords to a string or array.

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.