Familiar with SEO friends know that the site external links to the failure of the situation if the link with rel= "nofollow" property can avoid unnecessary losses. This article is an example of the implementation of PHP to the site content of the external link filtering method. Specifically as follows:
Problem Description: The original station many articles are excerpts of the external article, the article many links are either a long time has expired, or some of the test URL, such as: http://localhost/and so on, links more words, on the formation of many dead links in the station, This is very unfavorable to SEO optimization.
Workaround: you need to filter the content within the site and add the rel= "nofollow" attribute to the link that is not an internal link .
In this paper, the function of the filtering external link of WordPress is borrowed, and then it can be used.
The specific code is as follows:
External links Add nofllow $content content $domain The current site 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;
}
Call is good when called, and the following is the call demo
$a [' content '] = content_nofollow ($a [' content '], $domain); Add the link in the article content to the Nofllow property
Attention! This filter domain name needs to be without "/", such as Http://www.jb51.net, so that can be very good filtering.
I believe the method described in this article has a certain reference value for everyone's PHP project development.