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.
This example code mainly refers to the method for deleting external links in the CMS Content Management System.
Copy codeThe 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 ([^>] *)> (. *) <\/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;
}