Home  >  Article  >  Backend Development  >  How to filter other website links in html in php (domain name whitelist function)_PHP tutorial

How to filter other website links in html in php (domain name whitelist function)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:531124browse

复制代码 代码如下:

/**

* Filter external links
* @param array $local_domain Domain name array of this site
* @param string $message Text content
*/
    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);
    }

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/760287.htmlTechArticle复制代码 代码如下: /*** Filter external site links * @param array $local_domain Domain name array of this site * @param string $message text content*/ function replace_outer_links($local_do...
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