search
Homephp教程php手册PHP限制HTML内容中图片必须是本站的方法,

PHP限制HTML内容中图片必须是本站的方法,

本文实例讲述了PHP限制HTML内容中图片必须是本站的方法。分享给大家供大家参考。具体实现方法如下:

1. PHP代码如下:

<&#63;php
$dom = new DOMDocument;
$dom->loadHTML(file_get_contents('input.html'));
$xpath = new DOMXpath($dom);
$img = $xpath->query('//img');
foreach($img as $i) {
  $url = parse_url($i->getAttribute('src'));
  if(isset($url['host']) && in_array($url['host'], array('yourdomain.com', 'www.yourdomain.com')) == false) {
    // show an error
      // -- or --
      // remove the tag: $i->parent->removeChild($i)
    echo sprintf('[FAIL] %s' . PHP_EOL, $i->getAttribute('src'));
  }
  else {
    echo sprintf('[PASS] %s' . PHP_EOL, $i->getAttribute('src'));
  }
}

2. 测试HTML代码:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p><img  src="/static/imghwm/default1.png"  data-src="/image.jpg"  class="lazy" alt="PHP限制HTML内容中图片必须是本站的方法," ></p>
<p><img  src="/static/imghwm/default1.png"  data-src="http://www.bkjia.com/uploads/allimg/150618/00401323H-0.jpg"  class="lazy" alt="PHP限制HTML内容中图片必须是本站的方法," ></p>
<p><img  src="/static/imghwm/default1.png"  data-src="http://www.bkjia.com/uploads/allimg/150618/00401362U-1.jpg"  class="lazy" alt="PHP限制HTML内容中图片必须是本站的方法," ></p>
<p><img  src="/static/imghwm/default1.png"  data-src="http://otherdomain.com/image.jpg"  class="lazy" alt="PHP限制HTML内容中图片必须是本站的方法," ></p>

3. 运行结果:

[PASS] /image.jpg
[PASS] http://www.bkjia.com/uploads/allimg/150618/00401323H-0.jpg
[PASS] http://www.bkjia.com/uploads/allimg/150618/00401362U-1.jpg
[FAIL] http://otherdomain.com/image.jpg

希望本文所述对大家的php程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!