preg_match()使用正则表达式获取图像的源链接

时间:2019-01-10 11:19:36

标签: php regex preg-match

我想使用preg_match()函数从html内容中获取图像链接。

我试图这样做,但是没有获得正确的源链接。

$data = "<div class="poster">
<div class="pic">
<img class="xfieldimage img" src="https://bobtor.com/uploads/posts/2019-01/1546950927_mv5bnji5yta2mtetztmzny00odc5lwfimzctnme2owqwnwnkywm1xkeyxkfqcgdeqxvyntm3mdmymdq._v1_-1.jpg" alt="Song of Back and Neck 2018" title="Song of Back and Neck 2018">
</div>
</div>";

preg_match("'<img class=\"xfieldimage img\" src=\"(.*?)\" alt=\"(.*?)\" title=\"(.*?)\" />'si", $data, $movie_poster);

print_r($movie_poster);

它不起作用。

1 个答案:

答案 0 :(得分:2)

self-contained tags meme link

$dom = new DOMDocument();
$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
$image = $xpath->query("//img[@class='xfieldimage img']")->item(0);
echo $image->getAttribute("src");