Using regular expressions to extract the first image source from html codes?

后端 未结 10 1117
深忆病人
深忆病人 2020-12-05 01:07

I would like to know how this can be achieved.

Assume: That there\'s a lot of html code containing tables, divs, images, etc.

Problem: How can I get matches

10条回答
  •  执念已碎
    2020-12-05 01:33

    I really think you can not predict all the cases with on regular expression.

    The best way is to use the DOM with the PHP5 class DOMDocument and xpath. It's the cleanest way to do what you want.

    $dom = new DOMDocument();
    $dom->loadHTML( $htmlContent );
    $xml = simplexml_import_dom($dom);
    $images = $xml -> xpath('//img/@src');
    

提交回复
热议问题