You can use a simple regex to get it from the string, or use SimpleXMLElement
Example
<?php
$input = "<img src='http://www.example.com/Imagename.jpg' height='400px' width='600px' />";
$element= new SimpleXMLElement($input);
var_dump(basename((string) $element->attributes()->src));
Will output the desired result: string 'Imagename.jpg' (length=13)
Example using regular expression (i recommend use DOM or SimpleXMLElement like i've posted), but, if you want a simpler way like using a simple regex, you can do that:
<?php
preg_match("/src='(?P<url>([^']*?))'/", $input, $matches);
$filename = isset($matches['url']) ? basename($matches['url']) : null;
var_dump($filename);
It will produce the same output.
And a full example if you want scrap all HTML, you can do that:
<?php
$html = <<<HTML
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
</head>
<body>
lalala
<img src='http://www.example.com/Imagename.jpg' height='400px' width='600px' />
</body>
</html>
HTML;
function withEachImage ($html, callable $callback) {
libxml_use_internal_errors(true);
$document = new DOMDocument();
$document->loadHTML($html);
foreach ($document->getElementsByTagName('img') as $img) {
call_user_func_array($callback, array($img->getAttribute('src')));
}
}
withEachImage($html, function ($src) {
echo basename($src);
});