我有不同格式的 html 数组 [amp;src]=>image, anotherone [posthtml]=>image2, anothertwo [nbsp;image3
如何使用常用的preg_match()提取img和文本,通过它我们可以从html中得到完美的图像src和文本。如果无法使用 preg_match(),是否有另一种方法来修复它。 有知道的请回复一下。如何修复它。 我需要你的手。
最佳答案
推荐的方式是使用DOM
$dom = new DOMDocument;
$dom->loadHTML($HTML);
$images = $dom->getElementsByTagName('img');
foreach($images as $im){
$attrs = $imgages->attributes();
$src = $attrs->getNamedItem('src')->nodeValue
}
使用正则表达式:
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $m);
print_r($m);
关于php - 如何使用 preg_match 提取 img src,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289094/