Php automatically adds keyword links to articles.
- /**
- * Add keyword, internal link
- * By bbs.it-home.org
- */
- $ Link = array (
- Baidu, http://www.baidu.com /',
- 'Dedecms, http://bbs.it-home.org /',
- );
- $ Str = 'Search for dedecms in Baidu ';
- $ Out = keylink ($ str, $ link, 1 );
- // $ Str original character $ link, replacing the link array, 3 replications
- Echo $ out;
- Function _ sortDesc ($ a, $ B ){
- Return (strlen ($ a [0]) <strlen ($ B [0])? 1:-1;
- }
- Function keylink ($ str, $ link, $ count = 1 ){
- $ LinkDefs = $ link;
- $ LinkMap = array ();
- Foreach ($ linkDefs as $ row ){
- $ LinkMap [] = explode (',', $ row );
- }
- Foreach ($ linkMap as $ row ){
- $ Str = preg_replace ('/(s *)('. $ row [0]. ') (s *)/sui',' $ {2} ', $ str );
- }
- Usort ($ linkMap, '_ sortDesc ');
- $ TmpKwds = array ();
- Foreach ($ linkMap as $ I => $ row)
- {
- List ($ kwd, $ url) = $ row;
- For ($ j = $ I + 1; $ j <count ($ linkMap); $ j ++)
- {
- $ SubKwd = $ linkMap [$ j] [0];
- // If other keywords are included, replace them with other strings.
- If (strpos ($ kwd, $ subKwd )! = False)
- {
- $ TmpKwd = '{'. md5 ($ subKwd ).'}';
- $ Kwd = str_replace ($ subKwd, $ tmpKwd, $ kwd );
- $ TmpKwds [$ tmpKwd] = $ subKwd;
- }
- }
- // Replace text with link
- $ Str = preg_replace ('/('. $ row [0]. ')/sui','. $ kwd. '', $ str, $ count );
- }
- // Replace the string that replaces the subkeyword
- Foreach ($ tmpKwds as $ tmp => $ kwd ){
- $ Str = str_replace ($ tmp, $ kwd, $ str );
- }
- Return $ str;
- }
- ?>
|