Question about filtering uncivilized words in the message board content-php Tutorial

Source: Internet
Author: User
The question of filtering uncivilized words in the message board is as follows.

Nowadays, uncivilized words are an array. after the message board content, I need to use this uncivilized word array to find out whether there is a match in the message board content one by one. If yes, a prompt is displayed. How can we filter out uncivilized words?


Reply to discussion (solution)

First, you must have a dirty word package (database, or array form ). Then, replace the cyclic regular expression.

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 the uncivilized term '$ fw [$ I]'. enter"; break ;}} else {echo null ;}} again ;}} filterWd ("Find your sister ");


For example, filterword.txt contains the following three words: "echo", "your sister", and "find dead". your input content includes the uncivilized term '$ fw [$ I]'. please enter it again "; "This line will always output the word" uncivilized ". how can this be determined?

12 rows if ($ srs = false ){
Change to if ($ srs = false ){

12 rows if ($ srs = false ){
Change to if ($ srs = false ){



Well, I still don't think I can. However, after I changed the words to be filtered, I could find them. Now the question is, why are some words clearly not detected. For example

Function filterWd ($ content) {$ fw = array ("uncivilized", ""); for ($ I = 0; $ I <count ($ fw ); $ I ++) {$ srs = stripos ($ content, $ fw [$ I]); if ($ srs = false) {echo "error ". $ I. "Times
";}Else {echo" the content you entered contains the uncivilized term '$ fw [$ I]'. please enter "; break ;}}} filterWd (""); output: error 0th the content you entered contains the uncivilized term 'your ', please try again // why not first detect "uncivilized?

Didn't you find the first one and stop searching (break?

The first "uncivilized" is not equal to false output: the content you entered contains the uncivilized term 'your'. please enter it again. Then disconnect
Shouldn't it be like this? How can I jump to the second "go to you" to disconnect

The order of appearance is determined by your dictionary order.
If you use the trie algorithm (available in the essence area), you can check the order of input text.

Not very understandable. I just got in touch with it.

Your algorithm is to traverse the dictionary and check whether dictionary words appear in the body one by one.
That is, how many times should I check the dictionary?

The trie algorithm is to scan the body word for a word and check whether the word may appear in the dictionary.
You only need to scan the text to find all the dictionary words that exist.

You seem to understand this. I changed strstr () to solve this problem. strstr () should be full-text retrieval.

Unction filterWd ($ content) {$ fw = array ("Heaven", "Hell", "find death", "your sister", "uncivilized "); for ($ I = 0; $ I <count ($ fw); $ I ++) {$ srs = stristr ($ content, $ fw [$ I]); if ($ srs = false) {echo "error";} else {echo "your input contains uncivilized terms '$ fw [$ I]', enter "; break ;}}} filterWd (" uncivilized Ah go to your heaven to death ") again. output: the content you entered contains the uncivilized term 'Heaven ', please try again // no matter how disordered the order is in my array, the first "heaven" in the array is first retrieved in the body

If you use a single string to retrieve the body, strpos () should be faster.

The key is that I am retrieving the body from an array now. it is hard to understand whether the retrieved from the array matches.

If ($ srs = false ){
Echo "error No. $ I." times
";
} Else {
Echo "the content you entered contains the uncivilized term '$ fw [$ I]'. please enter it again ";
Break;
}

// The poster should have a good manual.

If ($ srs = false ){
Echo "error No. $ I." times
";
} Else {
Echo "the content you entered contains the uncivilized term '$ fw [$ I]'. please enter it again ";
Break;
}

// The poster should have a good manual.



There is one that I really don't understand. read the file and save it to the array.
Run to this step
(I have run echo $ fw [0... 9] Before the following line of code)
$ Srs = stristr ($ content, $ fw [$ I]); // This line is false.

The returned value is always false;

Really confusing

Use var_dump (); to output two values respectively.

Var_dump () has passed, as long as $ srs = stristr ($ content, $ fw [$ I]) has passed, all outputs are false.

Function filterWd ($ content) {$ fw = array ("Heaven", "Hell", "find death", "your sister", "uncivilized "); for ($ I = 0; $ I <count ($ fw); $ I ++) {$ srs = stristr ($ content, $ fw [$ I]); if ($ srs! = False) {echo "the content you entered contains the uncivilized term '$ fw [$ I]'. please enter it again.
"; // Break ;}}} filterWd (" uncivilized, go to your heaven to death ");
The content you entered contains the uncivilized term 'Heaven '. please enter it again
The content you entered contains the uncivilized term "find dead". please enter it again
The content you entered contains the uncivilized term 'uncivilized '. please enter it again

Previously, I wrote a php class to replace sensitive characters, including the whitelist and blacklist.
The blacklist is replaced *.

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

Function filterWd ($ content) {$ fw = array ("Heaven", "Hell", "find death", "your sister", "uncivilized "); for ($ I = 0; $ I <count ($ fw); $ I ++) {$ srs = stristr ($ content, $ fw [$ I]); if ($ srs! = False) {echo "the content you entered contains the uncivilized term '$ fw [$ I]'. please enter it again.
"; // Break ;}}} filterWd (" uncivilized, go to your heaven to death ");
The content you entered contains the uncivilized term 'Heaven '. please enter it again
The content you entered contains the uncivilized term "find dead". please enter it again
The content you entered contains the uncivilized term 'uncivilized '. please enter it again


I know this is acceptable. If the array is obtained by reading the file, it will not work.

The line break is not removed 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);

Just like this
$fn = "common/filterword.txt";$fw = file($fn, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);print_r($fw);

If the filter is used, can I publish it without prompting direct harmony?

Echo str_replace ($ swearing array, $ content );


This is the case. just remove the space line break.

$ Fw [] = trim (fgets ($ fn ));

Then retrieve strstr ($ content, $ fw [$ I]);

In this way, you can

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.