php去除外链的方法:首先打开相应的PHP文件;然后通过定义的“Replace_Links”方法处理下文章内容;最后完成文章外部链接的自动删除即可。
推荐:《PHP视频教程》
一般在做网站系统的时候,出于优化等因素的考虑需要再添加文章的时候删除掉不是本站的链接,对于这一要求可以通过让PHP处理下文章内容,来达到文章外部链接的自动删除的效果。
本实例代码主要参考织梦CMS内容管理系统的外链删除方法。
代码如下:
/** * 删除非站内链接 * * @access public * @param string $body 内容 * @param array $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; }
The above is the detailed content of How to remove external links in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version
