WordPress Version: 4.0
/**
* Retrieve all image URLs from given URI.
*
* @since 2.6.0
*
* @param string $uri
* @return string
*/
function get_images_from_uri($uri)
{
$uri = preg_replace('/\/#.+?$/', '', $uri);
if (preg_match('/\.(jpe?g|jpe|gif|png)\b/i', $uri) && !strpos($uri, 'blogger.com')) {
return "'" . esc_attr(html_entity_decode($uri)) . "'";
}
$content = wp_remote_fopen($uri);
if (false === $content) {
return '';
}
$host = parse_url($uri);
$pattern = '/<img ([^>]*)src=(\"|\')([^<>\'\"]+)(\2)([^>]*)\/*>/i';
$content = str_replace(array("\n", "\t", "\r"), '', $content);
preg_match_all($pattern, $content, $matches);
if (empty($matches[0])) {
return '';
}
$sources = array();
foreach ($matches[3] as $src) {
// If no http in URL.
if (strpos($src, 'http') === false) {
// If it doesn't have a relative URI.
if (strpos($src, '../') === false && strpos($src, './') === false && strpos($src, '/') === 0) {
$src = 'http://' . str_replace('//', '/', $host['host'] . '/' . $src);
} else {
$src = 'http://' . str_replace('//', '/', $host['host'] . '/' . dirname($host['path']) . '/' . $src);
}
}
$sources[] = esc_url($src);
}
return "'" . implode("','", $sources) . "'";
}