PHP Filter Specified keyword Implementation method summary _php tutorial

Source: Internet
Author: User
We often find that we have to submit to leave the place will have a lot of to send ads, and then want to do a screen filter to specify the function of the keyword, under my search for a few methods introduced to everyone needs to know the friend can refer to.

Ideas:
First, the key words specifically written in a text file, each line one, the number of unlimited, how much write how much.
Second, PHP read the keyword text, stored in an array
Third, traverse the key word group, and then use the Strpos function to see if there are no keywords, if any, return true, no then return false

The PHP code is as follows:

The code is as follows Copy Code

/* Filter keywords using strpos function in PHP */
Keyword Filter function
function Keywordcheck ($content) {
Remove whitespace
$content = Trim ($content);
Reading the keyword text
$content = @file_get_contents (' keyWords.txt ');
Convert an array
$arr = Explode ("n", $content);
Traverse detection
For ($i =0, $k =count ($arr), $i < $k; $i + +) {
If this array element is empty, skip this loop
if ($arr [$i]== ') {
Continue
}
If a keyword is detected, it returns a matching keyword and terminates the run
if (@strpos ($str, Trim ($arr [$i]))!==false) {
$i = $k;
return $arr [$i];
}
}
Returns False if no keyword is detected
return false;
}
$content = ' Here is the text content to be published ... ';
Filter keywords
$keyWord = Keywordcheck ($content);
Determine if a keyword exists
if ($keyWord) {
Echo ' Your post has keywords '. $keyWord;
}else{
Echo ' Congratulations! through keyword detection ';
The publish action can be completed by the write library operation down.
}

Example 2 (note: The keyword file used in Chinese keyword filtering is utf-8 encoded

The code is as follows Copy Code


/**
* Forbidden keyword Detection
*
* @param string $string to be detected
* @param string $fileName block the keyword file
* @return BOOL
*/
function Banwordcheck ($string, $fileName)
{
if (! ( $words = file_get_contents ($fileName))) {
Die (' file read error! ');
}
$string = Strtolower ($string);
$matched = Preg_match ('/'. $words. ' /I ', $string, $result);
if ($matched && isset ($result [0]) && strlen ($result [0]) > 0)
{
if (strlen ($result [0]) = = 2) {
$matched = Preg_match ('/'. $words. ' /iu ', $string, $result);
}
if ($matched && isset ($result [0]) && strlen ($result [0]) > 0) {
return true;
}else{
return false;
}
}else{
return false;
}
}

$content = ' Test keywords ';
if (Banwordcheck ($content, './banwords.txt ')) {
echo "matched!";
}else{
echo "No match!";
}

http://www.bkjia.com/PHPjc/629632.html www.bkjia.com true http://www.bkjia.com/PHPjc/629632.html techarticle we often find that we have to submit to leave the place will have a lot of to send ads, and then want to do a screen filter to specify the function of the keyword, under I searched several methods introduced to the big ...

  • 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.