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