Home  >  Article  >  Backend Development  >  PHP filters website links in HTML. PHP implements domain name whitelist function.

PHP filters website links in HTML. PHP implements domain name whitelist function.

WBOY
WBOYOriginal
2016-07-25 08:53:221234browse
  1. /**

  2. * Filter external site links
  3. * @param array $local_domain Domain name array of this site
  4. * @param string $message text content
  5. */
  6. function replace_outer_links($local_domain_arr, $message) {

  7. $pattern= '/<[^>]*href=['"]http[s]?://(?!' ;

  8. $i = 0 ;
  9. foreach ($local_domain_arr as $local_domain){
  10. if($i==0){
  11. $pattern .= 'www.' .$local_domain.'|'.$local_domain.'|[w_]+.'.$local_domain ;
  12. }else{
  13. $pattern .= '|www.' .$local_domain.'|'.$local_domain.'|[w_]+.'.$local_domain ;
  14. }
  15. $i++ ;
  16. } // bbs.it-home.org
  17. $pattern .=')[^'^"]*['"][^>]*>(.+?)/is';
  18. return preg_replace($pattern,'$1',$message);
  19. }

复制代码


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