get_images_from_uri

The timeline below displays how wordpress function get_images_from_uri has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

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) . "'";
}

WordPress Version: 3.9

/**
 * 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) . "'";
}

WordPress Version: 3.7

/**
 * Retrieve all image URLs from given URI.
 *
 * @package WordPress
 * @subpackage Press_This
 * @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) . "'";
}