Home >Backend Development >PHP Tutorial >php过滤html中的其他网站链接的方法(域名白名单功能)_PHP

php过滤html中的其他网站链接的方法(域名白名单功能)_PHP

WBOY
WBOYOriginal
2016-06-01 11:54:55847browse

复制代码 代码如下:
/**

     * 过滤外站链接
     * @param array $local_domain  本站域名 数组
     * @param string $message    文本内容
     */
    function replace_outer_links($local_domain_arr, $message) {

        $pattern= '/]*href=[\'\"]http[s]?:\/\/(?!' ;
        $i = 0 ;
        foreach ($local_domain_arr as $local_domain){
            if($i==0){
                 $pattern .= 'www.' .$local_domain.'|'.$local_domain.'|[\w\_]+\.'.$local_domain ;
            }else{
                 $pattern .= '|www.' .$local_domain.'|'.$local_domain.'|[\w\_]+\.'.$local_domain ;
            }
             $i++ ;
        }
        $pattern .=')[^\'^\"]*[\'\"][^>]*>(.+?)/is';  
        return preg_replace($pattern,'$1',$message);
    }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn