PHP Screen Filter The method of specifying keywords, php screen filter keyword _php Tutorial

Source: Internet
Author: User

PHP Screen Filter The method of specifying keywords, PHP screen filter keywords


The example in this article describes how PHP masks filter the specified keywords. Share to everyone for your reference. The specific analysis is as follows:

Implementation 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:

Copy the Code Code as follows:/* filter keywords in PHP with strpos function */
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)

Copy the Code code as follows:/**
* 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!";
}

I hope this article is helpful to everyone's PHP programming.


I want to block some sensitive words through PHP code, write a broken code, always wrong

Determine whether a paragraph of text contains a string or some string, can be judged by Preg_match, for the message book or forum, you can determine whether to include some sensitive keywords, to decide whether to filter, whether to allow publishing

Use the symbol "|" between each sensitive word. Split, because in the regular inside "|" Equivalent to the meaning of "or"


$badkey = "sensitive words | Sensitive words b| sensitive words c";

$string = "I do not contain sensitive words, I want to publish";

if (Preg_match ("/$badkey/I", $string)) {

echo "Sorry, contains sensitive characters, not allowed to publish";

}else{

Do something ...

}

?>

And preg_replace can be used for keyword filtering (profanity filtering)

PHP can use Preg_replace to filter dirty words


$badstring = "tmd| | tnnd| her mother's ";
$string = "What the hell you say, her Niang, not human";
echo preg_replace ("/$badstring/I", "', $string);

?>


$allergicWord = Array (' Dirty word ', ' curse words ');

$str = ' This sentence contains foul language and curse words ';

for ($i =0; $i <>
$content = Substr_count ($str, $allergicWord [$i]);
if ($content >0) {
$info = $content;
Break
}
}

if ($info >0) {
There are illegal characters
return TRUE;
}else{
No illegal characters
return FALSE;
}
?>

How does PHP use to implement keyword blocking? You can also use JS or Ajax


Version:

Html>
Head>
/head>
Form action= "method=" Post "Name=" Form1 "
Input Name= "word" type= "text"/>
input Name= "" type= "Submit" onclick= "return Chkvalue ();" value= "Confirm"/
/form>
Script type= "Text/javascript",
function Chkvalue () {
var val = Document.form1.word.value,
array = ["ACD", "SVD", "FDG", "FDK"];
for (var i = array.length; i--;) {
if (Val.indexof (Array[i])!==-1) {
Alert (' has sensitive characters: ' + array[i]);
return false;
}
}
return true;
}
/script>
/html>

php version
? php

$content = "AFJVAHSV sjv Sdjavcvacvdjgfsacdaldkfja SFCJASLDFJ ";
$keyword = Array ("ACD", "SVD", "FDG", "FDK");
for ($i = count ($keyword); $i--;) {
if (Strpos ($content, $keyword [$i])!== false) {
echo "appears a sensitive character: {$keyword [$i]}"; exit ();
}
}

?

Best days of modern times the new renewal
 

http://www.bkjia.com/PHPjc/904936.html www.bkjia.com true http://www.bkjia.com/PHPjc/904936.html techarticle PHP screen Filter To specify the method of the keyword, the PHP screen filter keywords This article describes the PHP mask filter The specified keyword method. Share to everyone for your reference. The specific analysis is as follows: ...

  • Related Article

    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.