Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // http://stackoverflow.com/questions/17769436/how-to-delete-my-image-host-is-not
- $a="<img src='myhost.com/img.jpg'/><img src='hosta.com/img.jpg'/><img src='http://hostb.com/img.jpg'/><img src='hostc.com/img.jpg'/>";
- $myhost='myhost.com';
- $doc = new DOMDocument('1.0'); //http://www.php.net/manual/en/class.domdocument.php#domdocument.props.documentelement
- $doc->loadHTML($a);
- $images = $doc->getElementsByTagName('img');
- $regex = '§([\w]+://)?(\www\d*\.)?(.*)§i'; // http://www.php.net/manual/en/regexp.introduction.php
- $domElemsToRemove = array();
- foreach ($images as $image) {
- $src = $image->getAttribute('src');
- preg_match($regex,$src,$m);
- //if ($m[1]=='') { $m[1] = 'http://'; } //optional
- //var_export($m);
- $pos = strpos($m[3], $myhost);
- if ($pos === false || $pos>0) {
- $domElemsToRemove[] = $image;
- }
- }
- foreach( $domElemsToRemove as $domElement ){
- $domElement->parentNode->removeChild($domElement); // http://www.php.net/manual/en/domnode.removechild.php#90292
- }
- $result = $doc->saveHTML();
- echo $result; // `<img src="myhost.com/img.jpg">`
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement