/*
- * Keyword Matching class
- * @ Author ylx
- * @ Packet mipang
- * Use instances
- * $ Str = "the green shell egg zafdensa's local army in the room will be scattered and scattered in the next year and in the next year ";
- * $ Key = new KeyReplace ($ str, array ("xxxx" => "sadf", "Next year 1" => 'http: // baidu.com ', "Next year" => 'Google. com '));
- * Echo $ key-> getResultText ();
- * Echo $ key-> getRuntime ();
- */
- Class KeyReplace
- {
- Private $ keys = array ();
- Private $ text = "";
- Private $ runtime = 0;
- Private $ url = true;
- Private $ stopkeys = array ();
- Private $ all = false;
- /**
- * @ Access public
- * @ Param string $ text specifies the article to be processed
- * @ Param array $ keys specifies the dictionary phrase array (key => url,...) url can be an array. if it is an array, a random replacement
- * @ Param array $ stopkeys specify the stop word array (key,...). the words in this field will not be processed.
- * @ Param boolean $ url true: replace the value with a link. otherwise, replace the value with only one link.
- * @ Param boolean $ all true indicates that all words found are replaced; otherwise, only the first time is replaced.
- */
- Public function _ construct ($ text = '', $ keys = array (), $ url = true, $ stopkeys = array (), $ all = false ){
- $ This-> keys = $ keys;
- $ This-> text = $ text;
- $ This-> url = $ url;
- $ This-> stopkeys = $ stopkeys;
- $ This-> all = $ all;
- }
/**
- * Get the processed articles
- * @ Access public
- * @ Return string text
- */
- Public function getResultText (){
- $ Start = microtime (true );
- $ Keys = $ this-> hits_keys ();
$ Keys_tmp = array_keys ($ keys );
Function cmp ($ a, $ B ){
- If (mb_strlen ($ a) = mb_strlen ($ B )){
- Return 0;
- }
- Return (mb_strlen ($ a) <mb_strlen ($ B ))? 1:-1;
- }
Usort ($ keys_tmp, "cmp ");
Foreach ($ keys_tmp as $ key ){
If (is_array ($ keys [$ key]) {
- $ Url = $ keys [$ key] [rand (0, count ($ keys [$ key])-1)];
- } Else
- $ Url = $ keys [$ key];
$ This-> text = $ this-> r_s ($ this-> text, $ key, $ url );
}
- $ This-> runtime = microtime (true)-$ start;
Return $ this-> text;
- }
- /**
- * Obtain the processing time
- * @ Access public
- * @ Return float
- */
- Public function getRuntime (){
Return $ this-> runtime;
}
/**
- * Set keywords
- * @ Access public
- * @ Param array $ keys array (key => url ,...)
- */
- Public function setKeys ($ keys ){
$ This-> keys = $ keys;
}
- /**
- * Set the stop word.
- * @ Access public
- * @ Param array $ keys array (key ,...)
- */
- Public function setStopKeys ($ keys ){
$ This-> stopkeys = $ keys;
}
- /**
- * Set an article
- * @ Access public
- * @ Param string $ text
- */
- Public function setText ($ text ){
$ This-> text = $ text;
}
/**
- * Used to find the hit keywords in the string
- * @ Access public
- * @ Return array $ keys returns the matched word array (key => url ,...)
- */
- Public function hits_keys (){
- $ Ar = $ this-> keys;
- $ Ar = $ ar? $ Ar: array ();
- $ Result = array ();
- $ Str = $ this-> text;
- Foreach ($ ar as $ k => $ url ){
- $ K = trim ($ k );
- If (! $ K)
- Continue;
- If (strpos ($ str, $ k )! = False &&! In_array ($ k, $ this-> stopkeys )){
- $ Result [$ k] = $ url;
- }
- }
- Return $ result? $ Result: array ();
- }
/**
- * Used to find the stop word hit in the string
- * @ Access public
- * @ Return array $ keys returns the matched word array (key ,...)
- */
- Public function hits_stop_keys (){
- $ Ar = $ this-> stopkeys;
- $ Ar = $ ar? $ Ar: array ();
- $ Result = array ();
- $ Str = $ this-> text;
- Foreach ($ ar as $ k ){
- $ K = trim ($ k );
- If (! $ K)
- Continue;
- If (strpos ($ str, $ k )! = False & in_array ($ k, $ this-> stopkeys )){
- $ Result [] = $ k;
- }
- }
- Return $ result? $ Result: array ();
- }
/**
- * Handling the replacement process
- * @ Access private
- * @ Param string $ text replaced
- * @ Param string $ key keyword
- * @ Param string $ url link
- * @ Return string $ text-processed articles
- */
- Private function r_s ($ text, $ key, $ url ){
$ Tmp = $ text;
$ Stop_keys = $ this-> hits_stop_keys ();
$ Stopkeys = $ tags = $ a = array ();
- If (preg_match_all ("#] +> [^ <] *] *> # Su ", $ tmp, $ m )){
- $ A = $ m [0];
Foreach ($ m [0] as $ k => $ z ){
- $ Z = preg_replace ("#\## s", "\#", $ z );
$ Tmp = preg_replace ('#'. $ z. '# s', "[_ a". $ k. "_]", $ tmp, 1 );
- }
};
If (preg_match_all ("# <[^>] +> # s", $ tmp, $ m )){
- $ Tags = $ m [0];
- Foreach ($ m [0] as $ k => $ z ){
- $ Z = preg_replace ("#\## s", "\#", $ z );
- $ Tmp = preg_replace ('#'. $ z. '# s', "[_ tag". $ k. "_]", $ tmp, 1 );
- }
- }
- If (! Empty ($ stop_keys )){
- If (preg_match_all ("#". implode ("|", $ stop_keys). "# s", $ tmp, $ m )){
- $ Stopkeys = $ m [0];
- Foreach ($ m [0] as $ k => $ z ){
- $ Z = preg_replace ("#\## s", "\#", $ z );
- $ Tmp = preg_replace ('#'. $ z. '# s', "[_ s". $ k. "_]", $ tmp, 1 );
- }
- }
- }
- $ Key1 = preg_replace ("# ([# \ (\) \ [\] \ *]) # s", "\\\\ $1", $ key );
If ($ this-> url)
- $ Tmp = preg_replace ("#(?! \ [_ S | \ [_ a | \ [_ | \ [_ t | \ [_ ta | \ [_ tag) ". $ key1 ."(?! Ag \ d + _ \] | g \ d + _ \] | s \ d + _ \] | _ \]) # us ", ''. $ key. '', $ tmp, $ this-> all? -1:1 );
- Else
- $ Tmp = preg_replace ("#(?! \ [_ S | \ [_ a | \ [_ | \ [_ t | \ [_ ta | \ [_ tag) ". $ key1 ."(?! Ag \ d + _ \] | g \ d + _ \] | s \ d + _ \] | _ \]) # us ", $ url, $ tmp, $ this-> all? -1:1 );
If (! Empty ($ )){
Foreach ($ a as $ n => $ ){
$ Tmp = str_replace ("[_ a". $ n. "_]", $ at, $ tmp );
}
}
- If (! Empty ($ tags )){
Foreach ($ tags as $ n =>$ ){
$ Tmp = str_replace ("[_ tag". $ n. "_]", $ at, $ tmp );
}
}
- If (! Empty ($ stopkeys )){
Foreach ($ stopkeys as $ n => $ ){
$ Tmp = str_replace ("[_ s". $ n. "_]", $ at, $ tmp );
}
}
- Return $ tmp;
- }
- }
|