PHP: how to filter external links of site content _ PHP

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.