Home > Article > Backend Development > Keyword replacement and link addition in php article content
/**
*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 = '/(?!(( <.*?)|(
$url=''.$keyword.'< ;/a>';
$content = preg_replace($regEx,$url,$content,1);
//Restore the keywords in the picture
$content=str_replace('%&&&&&%',$keyword,$content );
return $content;
}