


PHP filters external links and external images and adds the rel='nofollow' attribute_PHP tutorial
Jul 13, 2016 am 10:30 AMIt 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
Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The combination of Vue.js and ASP.NET provides tips and suggestions for performance optimization and expansion of web applications.

MySQL connection pool usage and optimization techniques in ASP.NET programs

Ten ways generative AI will change software development

The combination of Vue.js and ASP.NET enables the development and deployment of enterprise-level applications

How to reconnect to MySQL in ASP.NET program?

How to correctly configure and use MySQL connection pool in ASP.NET program?

Recommended configuration for ASP.NET development using Visual Studio on Linux

What are the built-in objects in aspnet?
