Message board content non-civilized word filtering problem

Source: Internet
Author: User
Title.

Now uncivilized words are the number of groups, in the message board content, I need to use this uncivilized word array inside to find whether there is a matching message board content. Prompt if there is one. How to realize the effect of this uncivilized word filter in the tangle


Reply to discussion (solution)

First you have to have a dirty word pack (database, or array form). It then loops the regular replacement.

function Filterwd ($content) {    $FN = "Common/filterword.txt";    $FD = fopen ($FN, "RB");    if (file_exists ($FN)) {        $FW = array ();        while (!feof ($FD)) {            $FW [] = Fgets ($FD);        }        for ($i = 0; $i < count ($FW), $i + +) {            $srs = Stripos ($content, $FW [$i]);            if ($srs = False) {                echo "error";            } else{                echo "The content you entered contains uncivilized words $FW [$i] ', please re-enter";                Break;}}}    Else{        echo null;    }} FILTERWD ("Your sister's looking for death");


For example, there are Filterword.txt, your sister, and death. Three words "echo" Your input contains uncivilized words ' $FW [$i] ', please re-enter ";" This line of output is always uncivilized this word ah, this how to judge?

12-row if ($srs = False) {
Change to if ($srs = = False) {

12-row if ($srs = False) {
Change to if ($srs = = False) {



Well, I changed it or I didn't think it was going to work. But after I changed a bit to filter the words can find out, and now the question, why some words are clearly some is not detected. Such as

function Filterwd ($content) {$FW = Array ("uncivilized", "Go to your"); for ($i = 0; $i < count ($FW); $i + +) {$srs = Stripos ($content, $FW [$ I]); if ($srs = = False) {echo "error". $i. "Secondary
";} Else{echo "What you have entered contains uncivilized words $FW [$i] ', please re-enter"; break;}}} FILTERWD ("Not civilized ah go to you"); output: Error The No. 0 time you entered the content contains uncivilized words ' go to your ', please re-enter//Why not first detected "uncivilized" it?

Didn't you find the first one to stop looking for (break)?

The first "uncivilized" is not equal to false output: You entered the content contains uncivilized words ' go to your ', please re-enter. and then disconnect
Shouldn't that be the case? How do you jump to the second "go to your" to disconnect

The order in which you appear is determined by your dictionary sequence.
If you use the trie algorithm (the Essence area has), you can follow the order of the input text check

Not very understanding, just contact soon

Your algorithm is to traverse the dictionary to see if the dictionary word is in the body
That is, how big the dictionary is, how many times must we check

And the trie algorithm is: word-by-word scan body, check whether the words composed of the occurrence in the dictionary
Just scan the text once, and you'll find all the dictionary words that exist.

It's a little indefinitely after you. I changed a strstr () to solve the problem, strstr () should be the full-text search

Unction filterwd ($content) {$FW = Array ("Heaven", "hell", "Find Dead", "Your Sister's", "uncivilized"); for ($i = 0; $i < count ($FW); $i + +) {$srs = Strist R ($content, $FW [$i]), if ($srs = = False) {echo "error";} Else{echo "What you have entered contains uncivilized words $FW [$i] ', please re-enter"; break;}}} FILTERWD ("Not civilized ah go to your heaven to find death"); output: Your input contains uncivilized words ' heaven ', please re-enter//No matter how I scrambled the array, the first "heaven" in the array was first retrieved in the body

If you just use a single string to retrieve the body with Strpos () the efficiency should be faster

The point is, I'm going to get the array to retrieve the body, and it's hard to understand if it's a match from the array.

if ($srs = = = False) {
echo "Error First". $i. "Secondary
";
}else{
echo "The content you entered contains uncivilized words $FW [$i] ', please re-enter";
Break
}

The landlord should take a good look at the handbook.

if ($srs = = = False) {
echo "Error First". $i. "Secondary
";
}else{
echo "The content you entered contains uncivilized words $FW [$i] ', please re-enter";
Break
}

The landlord should take a good look at the handbook.



There's one I really don't understand, read the file to be stored in the array
Run to this step
(I ran Echo $FW [0....9] Before this line of code).
$srs = Stristr ($content, $FW [$i]); This line is false.

The return value is forever false;

It's so confusing.

Use Var_dump () and output two values respectively to see.

Var_dump () also spent, as long as it was over $srs = Stristr ($content, $FW [$i]); The output is all false

function Filterwd ($content) {        $FW = array ("Heaven", "hell", "Find Dead", "Your Sister's", "uncivilized");        for ($i = 0; $i < count ($FW), $i + +) {            $srs = Stristr ($content, $FW [$i]);            if ($srs!== false) {                echo "your entry contains uncivilized words ' $FW [$i] ', please re-enter
"; Break;}}} FILTERWD ("Not civilized ah go to your heaven to find death");
The content you entered contains the uncivilized word ' paradise ', please re-enter
The content you entered contains the uncivilized phrase ' find dead ', please re-enter
The content you entered contains uncivilized words ' uncivilized ', please re-enter

A class that previously wrote a PHP replacement for sensitive characters, with white lists and blacklists.
The blacklist will be replaced with a *.

can refer to: http://blog.csdn.net/fdipzone/article/details/8486985

function Filterwd ($content) {        $FW = array ("Heaven", "hell", "Find Dead", "Your Sister's", "uncivilized");        for ($i = 0; $i < count ($FW), $i + +) {            $srs = Stristr ($content, $FW [$i]);            if ($srs!== false) {                echo "your entry contains uncivilized words ' $FW [$i] ', please re-enter
"; Break;}}} FILTERWD ("Not civilized ah go to your heaven to find death");
The content you entered contains the uncivilized word ' paradise ', please re-enter
The content you entered contains the uncivilized phrase ' find dead ', please re-enter
The content you entered contains uncivilized words ' uncivilized ', please re-enter


I know it's okay. Not if the array is read from the file.

You did not remove the line break when you read the file

$FN = "Common/filterword.txt"; $fd = fopen ($FN, "RB"), if (File_exists ($FN)) {   $FW = array ();   while (!feof ($FD)) {     $FW [] = Trim (fgets ($FD));}   } Print_r ($FW);

That's it.
$FN = "Common/filterword.txt"; $fw = File ($fn, File_ignore_new_lines | File_skip_empty_lines);p Rint_r ($FW);

Filter words ... Can not prompt direct harmony after the release?

Echo str_replace ($ dirty word array, $content);


This is really the case, you can remove the space line break

$FW [] = Trim (fgets ($FN));

Then retrieve the Strstr ($content, $FW [$i]);

That's it.

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