php去除外链的方法-PHP问题

资源魔 29 0

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;
}

以上就是php去除了外链的办法的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题 外链

抱歉,评论功能暂时关闭!