-1

I want to get the image link from the html content with preg_match() function.

I tried like this but not getting the correct source link.

$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);

Its not working.

Toto
  • 89,455
  • 62
  • 89
  • 125
SA CH
  • 31
  • 1
  • 6

1 Answers1

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");
mario
  • 144,265
  • 20
  • 237
  • 291
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592