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教程有兴趣的朋友有所帮助。
