PHP batch filter MYSQL database out-of-site links and pictures _ MySQL

Source: Internet
Author: User
PHP batch filtering of MYSQL database out-of-site links and images results in a large number of dead links because many links that reference out-of-site articles in the site are invalid. this is extremely unfriendly to search engines, this is not conducive to website optimization. Therefore, the out-of-site link filtering function is added to the website. 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

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 $ 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

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.