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

Keyword replacement and link addition in php article content

伊谢尔伦
Release: 2023-03-02 21:06:01
Original
2222 people have browsed it

/**
*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;
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template