Sensitive vocabulary filtering

Source: Internet
Author: User
Sensitive tokens are filtered. for example, the user input the parameter  strinput content 123104and the content is: about one thousand bad guys | 1 fire gun | 1 super bad guy | 1 .... how to use this txt file to check and filter $ str1; to improve efficiency, search for sensitive words
Assume that the user has entered
$ Str = "input 123 ";

Next I wrote abc.txt with about one thousand entries.
Bad guy | 1
Fire Gun | 1
Super bad guy | 1
....


How to use this txt file to check and filter $ str1; for efficiency, seek advice

Share: More


------ Solution --------------------
The ttrie class of http://bbs.csdn.net/topics/390221822 is used as an example.
Trie = new TTrie;
Foreach(file('abc.txt ') as $ r) $ trie-> set (strtok ($ r ,'
------ Solution --------------------
'));
$ S = <TXT
Input content: bad guy 123
TXT;
// Only recognize
Print_r ($ trie-> match ($ s ));
/* Get
Array
(
[0] => input content
[1] => bad guy
[2] => 123.
)
*/

// Remove dictionary words
$ Trie-> savematch = 0;
Print_r ($ trie-> match ($ s ));
/* Get
Array
(
[0] => input content
[1] => 123.
)
*/

Obviously, you have already met your needs.
When the output result is displayed, the connected array is a string.

If you want to highlight the keyword, you can do this.
Class mytrie extends ttrie {
Function B (){
$ This-> buffer [] =''. Array_pop ($ this-> buffer ).'';
}
}

$ Trie = new mytrie;
Foreach(file('abc.txt ') as $ r) $ trie-> set (strtok ($ r ,'
------ Solution --------------------
'),' B ');
$ S = <TXT
Input content: bad guy 123
TXT;
Print_r ($ trie-> match ($ s ));
/* Get
Array
(
[0] => input content
[1] =>Bad Guy= This is highlighted
[2] => 123.
)
*/

Actually, the fish has already been given to you, so it's better to do it yourself.

In order to reuse the instantiated object, we should also add at the beginning of the match method
    $this->buffer = array();
$this->input = 0;
$this->backtracking = 0;
To reinitialize the data pointer

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.