+
95
-

回答

将html文档中所有外部链接地址的href更换成本地的检查地址check.php?url=,代码如下:

<?php

function change_a_url($content = "") {
$pregRule = "/<[a|A].*?href=[\'|\"](.*?)[\'|\"].*?>(.*?)<\/[a|A]>/";
$content = preg_replace($pregRule, '<a href="check.php?url=${1}">${2}</a>', $content);
return $content;
}

$content = '什么,<a href="http://baidu.com">点击打开外链</a>,第二个链接<a href="http://baidu.com">点击打开外链</a>没了';
$newct = change_a_url($content);
var_dump($newct);

在check.php页面中提醒用户即将离开本站。

网友回复

我知道答案,我要回答