您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2026/2/6 18:21:00发布7次查看
本文实例讲述了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.

3. 运行结果:
[pass] /image.jpg[pass] http://yourdomain.com/image.jpg[pass] http://www.yourdomain.com/image.jpg[fail] http://otherdomain.com/image.jpg
转载自:http://www.aspnetjia.com
以上就介绍了php限制html内容中图片必须是本站的方法,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product