Php keyword replacement class (avoid repeated replacement, retain and restore the original link), php keyword _ PHP Tutorial

Source: Internet
Author: User
Php keyword replacement class (avoid repeated replacement, retain and restore the original link), php keyword. Php keyword replacement class (avoid repeated replacement, retain and restore the original link), php keyword reprint: www.169it.comblog_article601549531.html this section main content: a key php keyword replacement class (avoid repeated replacement, retain and restore the original link), php keyword

Reprinted: http://www.169it.com/blog_article/601549531.html

The main content of this section:
A keyword replacement class

It can be used for keyword filtering or keyword search replacement.

Implementation process analysis:
Keyword replacement is actually a str_replace () process. if it is a simple str_replace, it only takes about 2 seconds for A-word article.

Problem:
The keyword is replaced more than once. for example, a must be replaced with a, but the result may be.

Therefore, a method is required to protect the replaced tags. before processing the article, replace the label first, for example, [_ tnum _], and then restore it after the article has been processed.

Another problem is that if [_ tnum _] exists in a keyword or article, you need to exclude this. in this case, str_replace cannot be used, but preg_replace must be used for regular expression elimination.

The third question is what to do if there are two keywords a and AB. we hope to first match the long one and then the short one, so we need to sort it before matching.

In the last question, after str_replace is changed to preg_replace, it takes 5 seconds to match the same string for 10 times and strpos is faster in the string processing function. Then, use strpos to find the keyword, less than 1 second for 10 million queries. Even if it is 1 million, it takes more than 8 seconds.

A keyword matching replacement class, code:

Sample code:

/*
* 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;
}
}

Keywords (avoid repeated replacement, retain and restore the original link), php keywords Reprinted: http://www.169it.com/blog_article/601549531.html this section main content: a key...

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.