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

Source: Internet
Author: User

Because many links that reference documents outside the site are invalid, a large number of dead links are generated, which is unfriendly to search engines and is not conducive to website optimization, therefore, the out-of-site link filtering function is added to the site. For newly added articles, the rel = "nofollow" label is automatically added when the library is added, see "add external link Filtering for 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.53sj.net/data/attachment/block/d3/d34780d1fca3d6b7960a7eb7a2c4c0d3.jpg

After Batch Processing

The Code becomes <a href = "http://www.53sj.net/article-6-1.html" rel = "external nofollow"

 

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! <Br/> ';}}// 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;} original link: PHP batch filter MYSQL database out-of-site links and Images

Related Article

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.