Just get the image URL from string in PHP [duplicate]
From a string such as this <img src="/images/mylondon.jpg" />
I'm trying to retrieve JUST the url for use elsewhere in PHP
I know Regular expressions are the way to go, but I can't get my head around them right now.
Could anyone be of assistance?
preg_match_all('~<img.*?src=["\']+(.*?)["\']+~', $html, $urls);
$urls = $urls[1]
I think it would be better if used DOMDocument object:
$text = '<html><body><p>ala bala</p><img src="/images/mylondon.jpg" /></body></html>';
$htmlDom = new DOMDocument;
$htmlDom->loadHTML($text);
$imageTags = $htmlDom->getElementsByTagName('img');
$extractedImages = array();
foreach($imageTags as $imageTag){
$extractedImages[] = $imageTag->getAttribute('src');
}
echo '<pre>'; var_dump($extractedImages); exit;
精彩评论