Skip to content

Instantly share code, notes, and snippets.

@ihsanberahim
Created February 5, 2014 06:26
Show Gist options
  • Save ihsanberahim/8818316 to your computer and use it in GitHub Desktop.
Save ihsanberahim/8818316 to your computer and use it in GitHub Desktop.
extract image source attribute from html string in Drupal
/**
* Author: ihsanberahim
* Author Url: http://blog.powergeng.com
* Author Facebook: http://facebook.com/ihsanberahim
*
* i found it from stackoverflow
*/
function content2src($html){
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML( $html );
$xpath = new DOMXPath($doc);
$imgs = $xpath->query("//img");
for ($i=0; $i < $imgs->length; $i++) {
$img = $imgs->item($i);
$src = $img->getAttribute("src");
}
return $src;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment