PHP batch filter MYSQL database out-of-site links and Images

Source: Internet
Author: User
Adds filtering of external links to the site content. For filtering when data is called at the front-end, the speed at which the web page is opened and the server energy consumption increases a lot. Therefore, it is added when the data is stored. So what should we do with the existing data? Now you need to perform this operation on the original data. If you want to edit the data in the background

Adds filtering of external links to the site content. For filtering when data is called at the front-end, the speed at which the web page is opened and the server energy consumption increases a lot. Therefore, it is added when the data is stored. So what should we do with the existing data? Now you need to perform this operation on the original data. If you want to edit the data in the background

Adds filtering of external links to the site content. For filtering when data is called at the front-end, the speed at which the web page is opened and the server energy consumption increases a lot. Therefore, it is added when the data is stored.

So what should we do with the existing data? Now we need to perform this operation on the original data. If it is implemented by editing the data in the background one by one, even if we only need a few points, the amount of work is also very large, then we need a batch processing operation.

Write a batch processing program. After debugging and testing, the following program can replace the external links and external images in the original database.

For example, the site is http://www.ledaokj.com

A link in an article is http://www.53sj.net/article-6-1.html.

A picture is http://www.68idc.cn/help/uploads/allimg/150923/0I201BZ-0.jpg

After Batch Processing

The Code becomes

Batch filter MYSQL database internal and external links and image program code

Global $ config, $ db;

$ SQL = "SELECT 'id', 'content' FROM '{$ db-> prefix} article '";

$ A_list = $ db-> query ($ SQL );

$ Domain = $ config ['url'];

$ Domain = substr ($ domain, 0, strlen ($ domain)-1); // modify the current domain URL

Foreach ($ a_list as $ ){

$ Content = content_nofollow ($ a ['content'], $ domain );

Update_a ($ a ['id'], addslashes ($ content ));

}

Exit;

Function update_a ($ id, $ content ){

Global $ config, $ db;

$ SQL = "Update' {$ db-> prefix} article 'set 'content' = '{$ content} 'where 'id' = {$ id }";

If ($ db-> execute ($ SQL) {echo $ id. 'update successful!
';}

}

// Add nofllow $ content to external links $ domain 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 = "http :(.*?) "/', $ Content, $ matches );

If ($ matches ){

Foreach ($ matches [1] as $ val ){

If (strpos ($ val, $ domain) === false) $ content = str_replace ('src = "http :'. $ val. '"', 'src =" http :'. $ val. '"rel =" external nofollow "', $ content );

}

}

Return $ content;

}

PHP batch filter MYSQL database out-of-site links and Images

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.