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