PHP blocking methods for filtering specified keywords, php blocking for filtering keywords _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP blocks the method for filtering specified keywords, and php blocks the filtering keywords. PHP blocking filters specified keywords. This article describes how php blocking filters specified keywords. Share it with you for your reference. The specific analysis is as follows: PHP blocking filters specified keywords, php blocking filters keywords

This example describes how to block and filter specified keywords in PHP. Share it with you for your reference. The specific analysis is as follows:

Implementation ideas:

1. write keywords in a text file. each line has one keyword. the number of keywords is unlimited and the number of keywords is limited.
2. PHP reads the keyword text and stores it in an array
3. traverse the keyword array. use the strpos function one by one to check whether there are any keywords in the content. If yes, return true. If no, return false.

The PHP code is as follows:

The code is as follows:

/* Use the strpos function in PHP to filter keywords */
// Keyword filtering function
Function keyWordCheck ($ content ){
// Remove the blank
$ Content = trim ($ content );
// Read the keyword text
$ Content = @file_get_contents('keyWords.txt ');
// Convert to an array
$ Arr = explode ("n", $ content );
// Traversal detection
For ($ I = 0, $ k = count ($ arr); $ I <$ k; $ I ++ ){
// Skip this loop if the array element is null
If ($ arr [$ I] = ''){
Continue;
}
// If a keyword is detected, the matching keyword is returned and the running is terminated.
If (@ strpos ($ str, trim ($ arr [$ I])! = False ){
// $ I = $ k;
Return $ arr [$ I];
}
}
// If no keyword is detected, false is returned.
Return false;
}
$ Content = 'here is the text to be published... ';
// Filter keywords
$ KeyWord = keyWordCheck ($ content );
// Determine whether a keyword exists
If ($ keyWord ){
Echo 'Your published content has a keyWord '. $ keyWord;
} Else {
Echo 'Congratulations! Keyword detection ';
// You can write the database to complete the publishing.
}

Example 2 (note: The keyword file used for Chinese keyword filtering is UTF-8 encoded)

The code is as follows:

/**
* Forbidden keyword detection
*
* @ Param string $ string the string to be checked
* @ Param string $ fileName mask keyword files
* @ 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 keyword ';
If (banwordCheck ($ content, './banwords.txt ')){
Echo "matched! ";
} Else {
Echo "no match! ";
}

I hope this article will help you with PHP programming.


I want to use php code to block some sensitive words and write code disconnected, which is always wrong.

You can use preg_match to determine whether a text contains a certain string or some strings. for the message board or forum, you can determine whether it contains some sensitive keywords to determine whether to filter, allow posting?

Sensitive words are separated by the symbol "|", because "|" is equivalent to "or" in the regular expression.


$ Badkey = "sensitive word | sensitive word B | sensitive word C ";

$ String = "I do not include sensitive words. I want to post ";

If (preg_match ("/$ badkey/I", $ string )){

Echo "Sorry, it contains sensitive characters and cannot be published ";

} Else {

// Do something...

}

?>

Preg_replace can be used for keyword filtering (swearing filtering)

Php can use preg_replace to filter out swearing words.


$ Badstring = "tmd | mom | TNND | Girl's ";
$ String = "What are you talking about tmd? she is not a human ";
Echo preg_replace ("/$ badstring/I", '', $ string );

?>


$ AllergicWord = array ('swearing ', 'swearing ');

$ Str = 'This sentence contains swearing and swearing ';

For ($ I = 0; $ I $ Content = substr_count ($ str, $ allergicWord [$ I]);
If ($ content> 0 ){
$ Info = $ content;
Break;
}
}

If ($ info> 0 ){
// Contains invalid characters
Return TRUE;
} Else {
// No illegal characters
Return FALSE;
}
?>

How does php 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 = "OK"/>
/Form>
Script type = "text/javascript">
Function chkValue (){
Var val = document. form1.word. value,
Array = ["acd", "svd", "fdk"];
For (var I = array. length; I --;){
If (val. indexOf (array [I])! =-1 ){
Alert ('sensitive characters: '+ array [I]);
Return false;
}
}
Return true;
}
/Script>
/Html>

PHP version
? Php

$ Content = "afjvahsv sjv sdjavcvacvdjgfsacdaldkfja sfcjasldfj ";
$ Keyword = array ("acd", "svd", "di-"," fdk ");
For ($ I = count ($ keyword); $ I --;){
If (strpos ($ content, $ keyword [$ I])! = False ){
Echo "sensitive characters: {$ keyword [$ I]}"; exit ();
}
}

?>

The age of the hundred worlds is now a new age

Examples in this article describes how PHP shields and filters specified keywords. Share it with you for your reference. The specific analysis is as follows :...

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.