Extract Image Sources from text in PHP – preg_match_all required

I have a little issue as my preg_match_all is not running properly.

what I want to do is extract the src parameter of all the images in the post_content from the wordpress which is a string – not a complete html document/DOM (thus cannot use a document parser function)

Read More

I am currently using the below code which is unfortunately too untidy and works for only 1 image src, where I want all image sources from that string

preg_match_all( '/src="([^"]*)"/', $search->post_content, $matches);

if ( isset( $matches ) )
{  

foreach ($matches as $match) 
{

if(strpos($match[0], "src")!==false)
{
$res = explode(""", $match[0]);
echo $res[1];
}

}

}

can someone please help here…

Related posts

Leave a Reply

2 comments

  1. Using regular expressions to parse an HTML document can be very error prone. Like in your case where not only IMG elements have an SRC attribute (in fact, that doesn’t even need to be an HTML attribute at all). Besides that, it also might be possible that the attribute value is not enclosed in double quote.

    Better use a HTML DOM parser like PHP’s DOMDocument and its methods:

    $doc = new DOMDocument();
    $doc->loadHTML($search->post_content);
    foreach ($doc->getElementsByTagName('img') as $img) {
        if ($img->hasAttribute('src')) {
            echo $img->getAttribute('src');
        }
    }