首页  >  文章  >  后端开发  >  php文章内容中的关键词替换加链接

php文章内容中的关键词替换加链接

伊谢尔伦
伊谢尔伦原创
2016-12-02 11:04:202170浏览

/** 
*对内容中的关键词添加链接 
*只处理第一次出现的关键词,对已有链接的关键不会再加链接,支持中英文 
*$content:string 原字符串 
*$keyword:string  关键词 
*$link:string,链接 
*/ 
public static function yang_keyword_link($content,$keyword,$link){ 
//排除图片中的关键词 
$content = preg_replace( '|(]*?)('.$keyword.')([^>]*?>)|U', '$1%&&&&&%$3', $content); 
$regEx = '/(?!((<.*?)|(]*?)>)|([^>]*?<\/a>))/si'; 
$url=''.$keyword.''; 
$content = preg_replace($regEx,$url,$content,1); 
//还原图片中的关键词 
$content=str_replace('%&&&&&%',$keyword,$content); 
return $content; 
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn