PHP限制HTML內(nèi)容中圖片必須是本站的方法
更新時(shí)間:2015年06月16日 17:26:56 作者:紅薯
這篇文章主要介紹了PHP限制HTML內(nèi)容中圖片必須是本站的方法,涉及對URL中域名的判定技巧,需要的朋友可以參考下
本文實(shí)例講述了PHP限制HTML內(nèi)容中圖片必須是本站的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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代碼:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> <p><img src="/image.jpg"></p> <p><img src="http://yourdomain.com/image.jpg"></p> <p><img src="http://www.yourdomain.com/image.jpg"></p> <p><img src="http://otherdomain.com/image.jpg"></p>
3. 運(yùn)行結(jié)果:
[PASS] /image.jpg [PASS] http://yourdomain.com/image.jpg [PASS] http://www.yourdomain.com/image.jpg [FAIL] http://otherdomain.com/image.jpg
希望本文所述對大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php編程實(shí)現(xiàn)追加內(nèi)容到txt文件中的方法
這篇文章主要介紹了php編程實(shí)現(xiàn)追加內(nèi)容到txt文件中的方法,涉及php覆蓋式添加內(nèi)容到txt文件、追加內(nèi)容以及逐行讀取txt文件的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08PHP精確到毫秒秒殺倒計(jì)時(shí)實(shí)例詳解
這篇文章主要介紹了PHP精確到毫秒秒殺倒計(jì)時(shí)實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03