本文实例讲述了php限制html内容中图片必须是本站的方法。分享给大家供大家参考。具体实现方法如下:
1. PHP代码如下:
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代码:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
立即学习“PHP免费学习笔记(深入)”;
传媒公司模板(RTCMS)1.0下载传媒企业网站系统使用热腾CMS(RTCMS),根据网站板块定制的栏目,如果修改栏目,需要修改模板相应的标签。站点内容均可在后台网站基本设置中添加。全站可生成HTML,安装默认动态浏览。并可以独立设置SEO标题、关键字、描述信息。源码包中带有少量测试数据,安装时可选择演示安装或全新安装。如果全新安装,后台内容充实后,首页才能完全显示出来。(全新安装后可以删除演示数据用到的图片,目录在https://
3. 运行结果:
[PASS] /image.jpg [PASS] http://yourdomain.com/image.jpg [PASS] http://www.yourdomain.com/image.jpg [FAIL] http://otherdomain.com/image.jpg















