Generally, when creating a website system, you need to delete the link that is not on this site when adding an article due to optimization or other factors. for this requirement, you can let PHP process the article content, to automatically delete external links. For more information about website systems, you can delete links that are not on this site when you want to add additional articles for optimization and other reasons, 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;
}