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