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