如何使用preg_match提取img src

Ank*_*dra 3 php

我有不同格式的html [amp; src] => image,另一个[posthtml] => image2,anothertwo [nbsp; image3

如何使用常见的preg_match()提取img和文本,通过它我们可以从html中获得完美的图像src和文本.如果使用preg_match()是不可能的,还有其他方法可以解决它.如果有人知道,请回复.如何解决它.我需要你的手.

Shi*_*dim 9

推荐的方法是使用DOM

$dom = new DOMDocument;
$dom->loadHTML($HTML);
$images = $dom->getElementsByTagName('img');

foreach($images as $im){
    $attrs = $imgages->attributes();
    $src = $attrs->getNamedItem('src')->nodeValue
}
Run Code Online (Sandbox Code Playgroud)

使用正则表达式:

preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $m); 
print_r($m);
Run Code Online (Sandbox Code Playgroud)