Jetpack_PostImages::from_html PHP Method

from_html() static public method

Very raw -- just parse the HTML and pull out any/all img tags and return their src
static public from_html ( mixed $html_or_id ) : Array
$html_or_id mixed The HTML string to parse for images, or a post id
return Array containing images
    static function from_html($html_or_id)
    {
        $images = array();
        if (is_numeric($html_or_id)) {
            $post = get_post($html_or_id);
            if (empty($post) || !empty($post->post_password)) {
                return $images;
            }
            $html = $post->post_content;
            // DO NOT apply the_content filters here, it will cause loops
        } else {
            $html = $html_or_id;
        }
        if (!$html) {
            return $images;
        }
        preg_match_all('!<img.*src=[\'"]([^"]+)[\'"].*/?>!iUs', $html, $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $match) {
                if (stristr($match, '/smilies/')) {
                    continue;
                }
                $images[] = array('type' => 'image', 'from' => 'html', 'src' => html_entity_decode($match), 'href' => '');
            }
        }
        return $images;
    }

Usage Example

 /**
  * @author blobaugh
  * @covers Jetpack_PostImages::from_html
  * @since 2.7
  */
 public function test_from_html_double_quotes()
 {
     $s = "<imgANYTHINGATALLHEREsrc='bob.jpg'MOREANYTHINGHERE/>";
     $result = Jetpack_PostImages::from_html($s);
     $this->assertInternalType('array', $result);
     $this->assertFalse(empty($result));
 }
All Usage Examples Of Jetpack_PostImages::from_html