Home  >  Article  >  Backend Development  >  Keyword replacement and link addition in php article content

Keyword replacement and link addition in php article content

伊谢尔伦
伊谢尔伦Original
2016-12-02 11:04:202206browse

/**
*Add links to keywords in the content
*Only process the keywords that appear for the first time. Keywords that already have links will not be linked again. Support Chinese and English
*$content:string original string
*$keyword :string Keywords
*$link:string, link
*/
public static function yang_keyword_link($content,$keyword,$link){
//Exclude keywords in the picture
$content = preg_replace( '|( ]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content);
$regEx = '/(?!(( <.*?)|(]*?)>)|([^>] *?))/si';
$url=''.$keyword.'< ;/a>';
$content = preg_replace($regEx,$url,$content,1);
//Restore the keywords in the picture
$content=str_replace('%&&&&&%',$keyword,$content );
return $content;
}