1 2 3 4 5 6 7 8 9 10 | function yang_keyword_link( $content , $keyword , $link ){ //排除图片中的关键词 $content = preg_replace( '|(<img[^>]*?)(' . $keyword . ')([^>]*?>)|U' , '$1%&&&&&%$3' , $content ); $regEx = '/(?!((<.*?)|(<a.*?)))(' . $keyword . ')(?!(([^<>]*?)>)|([^>]*?<\/a>))/si' ; $url = '<a href="' . $link . '" target="_blank" class="content_guanjianci">' . $keyword . '</a>' ; $content = preg_replace( $regEx , $url , $content ,1); //还原图片中的关键词 $content = str_replace ( '%&&&&&%' , $keyword , $content ); return $content ; } |