时间:2021-07-01 10:21:17 帮助过:3人阅读
1. PHP代码如下:
<?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代码:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
3. 运行结果:
[PASS] https://www.gxlcms.com/image.jpg [PASS] http://yourdomain.comhttps://www.gxlcms.com/image.jpg [PASS] http://www.yourdomain.comhttps://www.gxlcms.com/image.jpg [FAIL] http://otherdomain.comhttps://www.gxlcms.com/image.jpg
希望本文所述对大家的php程序设计有所帮助。