thinkphp article keywords automatic link-internal chain module

Source: Internet
Author: User

thinkphp article keywords automatic link-Internal connection module
First set up a keyword table named Xx_sitelink

Table structure ID link keyword

Create a replacement link module by creating a new ReplaceAction.class.php file under the foreground item

<?PHPclassReplaceactionextendsaction{Private $text; Public function__tostring () {return $this->replace ();}//Return Replacement Results Public functionContent$text){$this->text=$text;}//get content to $textPrivate functionReplace () {//Replace function   $keywords=$this-keywords (); $pattern=implode(' | ',Array_keys($keywords)); $pattern= "/((?<!<)) ($pattern)(?! [^<>]* (?:>|<\/a>))/"; return Preg_replace_callback($pattern,Array($this,Callback),$this-text);}Private function Callback($matches) {//Regular replacement callback function   Global $log;//Replace record, replace it without making the same substitution   if($log[$matches[2]])   return $matches[0]; $log[$matches[2]] =true; $keywords=$this-keywords (); $link=$keywords[$matches[2]]; return"<a href= '$link' >$matches[2]</a> ";}Private functionKeywords () {//Find Keywords   $Keywords= M ("SiteLink"); $Keywords=$Keywords->where ("link<>")Select (); foreach($Keywords  as $key=$value) {    if(Strpos($this->text,$value[keyword])) {//content contains keywords to be stored in an array     $m[$value[keyword]] =$value[link]; }   }   Krsort($m);//inverse sorting of arrays, longest keyword overrides   return $m;}} Reduced use of the same project$Form= M (' text ');$data=$Form->find ($id);if($data) {    $Replace= A ("Replace"); $Replace->content ($data[content]);//$data [content] for your positive text segment    $data[content]=$Replace;}$this-Assign (' Data ',$data);?>

thinkphp Article Keywords Auto link-inner chain module

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.