Home >Backend Development >PHP Problem >How to remove external links in php

How to remove external links in php

coldplay.xixi
coldplay.xixiOriginal
2020-09-01 13:35:512652browse

php method to remove external links: add the [rel="nofollow"] attribute to the link, the code is [$a['content'] = content_nofollow($a['content'],$domain) ;].

How to remove external links in php

[Related learning recommendations: php graphic tutorial]

php method to remove external links:

Solution: It is necessary to filter the content within the site, and add rel=" to links that are not internal links nofollow" attribute.

This article draws on wordpress’s function for filtering external links, and you can use it by changing it.

The specific code is as follows:

//外部链接增加nofllow $content 内容 $domain 当前网站域名
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);  //将文章内容里的链接增加nofllow属性

Related learning recommendations: php programming (video)

The above is the detailed content of How to remove external links in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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