PHP implementation of the site content of the external link filtering method _php skills

Source: Internet
Author: User

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.

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.