Heim  >  Artikel  >  Backend-Entwicklung  >  So entfernen Sie externe Links in PHP

So entfernen Sie externe Links in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-09-01 13:35:512555Durchsuche

So entfernen Sie externe Links in PHP: Fügen Sie dem Link das Attribut [rel="nofollow"] hinzu. Der Code lautet [$a['content'] = content_nofollow($a['content'],$domain); ].

So entfernen Sie externe Links in PHP

【Verwandte Lernempfehlungen: php-Grafik-Tutorial

php-Methode zum Entfernen externer Links:

Lösung: Der Inhalt der Website muss gefiltert werden Sind keine internen Links, fügen Sie das Attribut rel="nofollow" hinzu.

Dieser Artikel basiert auf der Funktion von WordPress zum Filtern externer Links und Sie können sie verwenden, indem Sie sie ändern.

Der spezifische Code lautet wie folgt:

//外部链接增加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;
}

Es ist einfach anzurufen, wenn Sie anrufen. Das Folgende ist eine Aufrufdemonstration

$a['content'] = content_nofollow($a['content'],$domain);  //将文章内容里的链接增加nofllow属性

Verwandte Lernempfehlungen: php-Programmierung(Video)

Das obige ist der detaillierte Inhalt vonSo entfernen Sie externe Links in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn