PHP implementation of deleting non-station internal and external links instance code _ PHP Tutorial

Source: Internet
Author: User
PHP deletes the code of an instance that does not link to the internal and external sites. Generally, when creating a website system, you need to delete a link that is not on this site when adding an article due to optimization or other factors, for this requirement, you can let PHP handle the website system in the next article, and delete the link not on this site when you need to add the article for optimization and other factors, for this requirement, PHP can process the content of the article to achieve the automatic deletion of the article's external links.

This example code mainly refers to the method for deleting external links in the CMS content management system.

The code is as follows:

/**
* Delete a non-site link
*
* @ Access public
* @ Param string $ body content
* @ Param array $ hyperlinks allowed by allow_urls
* @ Return string
*/
Function Replace_Links (& $ body, $ allow_urls = array ())
{
$ Host_rule = join ('|', $ allow_urls );
$ Host_rule = preg_replace ("# [\ n \ r] #", '', $ host_rule );
$ Host_rule = str_replace ('.', "\.", $ host_rule );
$ Host_rule = str_replace ('/', "\/", $ host_rule );
$ Arr = '';
Preg_match_all ("#] *)> (. *) <\/a> # iU", $ body, $ arr );
If (is_array ($ arr [0])
{
$ Rparr = array ();
$ Tgarr = array ();
Foreach ($ arr [0] as $ I => $ v)
{
If ($ host_rule! = ''& Preg_match ('#'. $ host_rule. '# I', $ arr [1] [$ I])
{
Continue;
} Else {
$ Rparr [] = $ v;
$ Tgarr [] = $ arr [2] [$ I];
}
}
If (! Empty ($ rparr ))
{
$ Body = str_replace ($ rparr, $ tgarr, $ body );
}
}
$ Arr = $ rparr = $ tgarr = '';
Return $ body;
}


...

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.