This article mainly introduces how PHP can filter external links of site content. it can add the relnofollow function for external links and help SEO construction and avoid unnecessary losses, it is a very practical skill. if you need it, you can refer to any SEO-related friends who know it, if the link contains the rel = "nofollow" attribute when the external link of the website is invalid, unnecessary losses can be avoided. This article demonstrates how PHP can filter external links of site content in the form of an example. The details are as follows:
Problem description:In the past, many posts on the site were extracted from external articles. many links in the article either failed for a long time or some testing websites, such as http: // localhost/and so on. if there are too many links, many dead links will be formed in the site, which is very unfavorable for SEO optimization.
Solution:You need to filter the content in the site and add the link that is not an internal link to the rel = "nofollow" attribute.
This article draws on wordpress's function for filtering external links and uses it with a few changes.
The code is as follows:
// Add nofllow $ content $ domain the 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 well called during the call. The following is a call demonstration.
$ A ['content'] = content_nofollow ($ a ['content'], $ domain); // add the nofllow attribute to the link in the article content
Note! Here the domain name to be filtered does not contain "/", such as http://www.bitsCN.com, so that it can be good filtering.
I believe that the method described in this article has some reference value for PHP project development.