Home > Backend Development > PHP Tutorial > PHP filters external links and external images and adds the rel='nofollow' attribute_PHP tutorial

PHP filters external links and external images and adds the rel='nofollow' attribute_PHP tutorial

WBOY
Release: 2016-07-13 10:30:42
Original
1286 people have browsed it

It turns out that many of the articles on the site are excerpts from external articles. Many links in the articles have either expired over time, or they are some test URLs, such as: http://localhost/, etc., with many links. If not, it will form a lot of dead links in the site, which is very detrimental to SEO optimization. Then you need to filter the content within the site and add the rel="nofollow" attribute to links that are not internal links.

I found wordpress’s function for filtering external links on the Internet, just change it and you can use it

//Add nofllow to external links $content content $domain current website domain name function content_nofollow($content,$domain){ preg_match_all('/href="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,$domain)===false ) $content=str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ',$content); } } preg_match_all('/src="(.*?)"/',$content,$matches); if($matches){ foreach($matches[1] as $val){ if( strpos($val,$domain)===false ) $content=str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ',$content); } } return $content; } ​ It is easy to call when calling. The following is a calling demonstration. ​ $a['content'] = content_nofollow($a['content'],$domain); //Add the nofllow attribute to the links in the article content ​ Notice! The filtered domain name needs to be without "/", such as http://www.ledaokj.com ​ This allows for good filtering. ​ Original link: Add filtering for external links to site content

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764698.htmlTechArticleIt turns out that many articles on the site are excerpts from external articles. Many links in the articles have either expired over time, or Just some test URLs, such as: http://localhost/, links...
Related labels:
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