Php implements adding internal links with keyword in the article content

Source: Internet
Author: User
Tags foreach explode learn php


Example 1

The code is as follows: Copy code

/**
* Add links to keywords in the content
* Only processes the keywords that appear for the first time, and does not add links to the existing links. It supports both Chinese and English
* $ Content: string original string
* $ Keyword: string keyword
* $ Link: string, link
*/
Function yang_keyword_link ($ content, $ keyword, $ link ){
// Exclude keywords from the image
$ Content = preg_replace ('| (] *?) ('. $ Keyword.') ([^>] *?>) | U', '$ 1% & % $3', $ content );
$ RegEx = '/(?! (<.*?) | (<.*?))) ('. $ Keyword .')(?! ([^ <>] *?)>) | ([^>] *? </A>)/si ';
$ Url = '<a href = "'. $ link. '">'. $ keyword. '</a> ';
$ Content = preg_replace ($ regEx, $ url, $ content, 1 );
// Restore the keywords in the image
$ Content = str_replace ('% & %', $ keyword, $ content );
Return $ content;
}


Example 2

The code is as follows: Copy code


Include_once (dirname (_ file _). "/../db/DBViewSpot. php ");

Class InnerLink {
Private static $ spotUrlMap;
/**
* Generate view spots keywords link
     *
* @ Param string $ description
* @ Param array $ spotUrlMap
* @ Return string
*/
Public static function genSpotLink ($ basePath, $ description)
    {
If (empty (InnerLink: $ spotUrlMap )){
InnerLink: $ spotUrlMap = DBViewSpot: getSpotPare ();
        }
// Exclude irregular data
If (empty ($ description )){
Return $ description;
        }
Foreach (InnerLink: $ spotUrlMap AS $ spotUrlPair ){
$ Replace = "<a target = '_ blank' href = 'http: // pzg412403.blog.163.com/blog /". $ basePath. "/". $ spotUrlPair [1]. ". html '> ". $ spotUrlPair [0]. "</a> ";
// The description contains only text and no image, so pay attention to the link.
$ Tmp1 = explode ("<a", $ description );
$ Is_replaced = false;
Foreach ($ tmp1 as $ key => $ item ){
$ Tmp2 = explode ("</a>", $ item );
If (sizeof ($ tmp2)> 1 ){
If (substr ($ tmp2 [0], 0, 1 )! = "A" & substr ($ tmp2 [0], 0, 1 )! = ""){
If ($ is_replaced === false ){
$ Tmp2 [1] = InnerLink: str_replace_once ($ spotUrlPair [0], $ replace, $ tmp2 [1], $ is_replaced );
                        }
$ Tmp1 [$ key] = implode ("</a>", $ tmp2 );
                    }
} Else {
If (is_string ($ item) & $ is_replaced === false ){
$ Tmp1 [$ key] = InnerLink: str_replace_once ($ spotUrlPair [0], $ replace, $ item, $ is_replaced );
                    }
                }
            }
$ Description = implode ("<a", $ tmp1 );
        }
Return $ description;
    }
/**
* Replace key word for one time
     *
* @ Param string $ needle
* @ Param string $ replace
* @ Param string $ haystack
* @ Param bool $ is_replaced
* @ Return string
*/
Private static function str_replace_once ($ needle, $ replace, $ haystack, & $ is_replaced ){
$ Pos = strpos ($ haystack, $ needle );
If ($ pos = false ){
Return $ haystack;
        }
$ Is_replaced = true;
Return substr_replace ($ haystack, $ replace, $ pos, strlen ($ needle ));
    }
}

Example 3

This was written when I first learned php, and it felt a bit problematic.

The code is as follows: Copy code
<? Php
$ Keys = array (
Array ('webpage effect ', 'http: // www.111cn.net/js_a/js.html '),
Array ('SEO ', 'http: // www.111cn.net/seo/seo.html '),
Array ('php', '/phper/php.html '),
Array ('JSP ','/jsp/jsp.html '),
Array ('asp ','/asp/asp.html '),
Array ('ps', 'http: // www.111cn.net/fw/photo.html '),
Array ('Photoshop ', 'http: // www.111cn.net/fw/photo.html '),
Array ('javascript ', 'http: // www.111cn.net/js_a/js.html '),
Array ('. Net','/net/net.html '),
Array ('nonmainstream ', 'http: // www.111cn.net/fw/photo.html '),
Array ('Network', 'http: // www.111cn.net/mon/mon.html '),
Array ('css ','/cssdiv/css.html '),
Array ('Graphic Design ', 'http: // www.111cn.net/fw/photo.html '),
Array ('Website', '/person /'),
Array ('webpage maker ', 'http: // www.111cn.net/wy/yunchtml '),
Array ('search engine ', 'http: // www.111cn.net/seo/seo.html '),
Array ('Optimization', 'http: // www.111cn.net/seo/seo.html '),
Array ('animate', '/flash_a/flash.html '),
Array ('database', '/database/database.html '),
Array ('earning money ', 'http: // www.111cn.net/mon/mon.html '),
Array ('operation', 'http: // www.111cn.net/mon/mon.html ')
           
);
         
$ Str = "today is July 22, May 30, 2010. My website encountered problems. There are many seo problems. seo is the optimization of search engines. I used to learn php well. Now I think jsp is good, css + div, webpage, webpage design, webpage creation, webpage learning, webpage teaching, Photoshop, Flash, HTML, CSS, Dreamweaver, Fireworks, ASP, PHP, JSP, ASP. NET, website construction, website development, webpage special effects, graphic design, personal website, webpage materials ";
    
    
Echo $ str, "<br> ";
Foreach ($ keys as $ nkeys ){
// Print_r ($ nkeys); echo "<br> ";
// Foreach ($ nkeys as $ join ){
// Echo ($ join), "<br> ";
If (strpos ($ str, $ nkeys [0]) {
$ Str = str_replace ($ nkeys [0], "<a href = http://www.111cn.net ". $ nkeys [1]. "target = _ blank> ". $ nkeys [0]. "</a>", $ str );
           } 
//}
    } 
         
Echo $ str;
?>

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.