0

I draw data with the bot, but I can get it in html. I want to separate the url in html I tried it with explode() but I didn't succeed.

part of the code I want to get url with php

<img src="https://lh3.googleusercontent.com/D4r0FAqHl8HpymNXL78pSjskKl1f1WDU4jGIpTI0MiGR8xRfOCk5TiJH8dn9MdKEoyw=s100" srcset="https://lh3.googleusercontent.com/D4r0FAqHl8HpymNXL78pSjskKl1f1WDU4jGIpTI0MiGR8xRfOCk5TiJH8dn9MdKEoyw=s200 2x" class="T75of sHb2Xb" aria-hidden="true" alt="Kapak resmi" itemprop="image">
Thuy Rayn
  • 33
  • 6

1 Answers1

1

you can use the Dom Parser. Dom Parser is very good at dealing with XML as well as HTML

<?php
$html_string = '<img src="https://lh3.googleusercontent.com/D4r0FAqHl8HpymNXL78pSjskKl1f1WDU4jGIpTI0MiGR8xRfOCk5TiJH8dn9MdKEoyw=s100" srcset="https://lh3.googleusercontent.com/D4r0FAqHl8HpymNXL78pSjskKl1f1WDU4jGIpTI0MiGR8xRfOCk5TiJH8dn9MdKEoyw=s200 2x" class="T75of sHb2Xb" aria-hidden="true" alt="Kapak resmi" itemprop="image">';

//Create a new DOM document
$doc = new \DOMDocument();

 //Parse the HTML.
$doc->loadHTML($html_string);

//Get the image tag to parse
$img = $doc->getElementsByTagName("img"); // DOMNodeList Object

//Craete array to add all DOMElement value
$image = array();
$i= 0;
foreach($img as $item) { // DOMElement Object
    //DOMElement::getAttribute — Returns value of attribute
    $image[$i]['img']['src'] = $item->getAttribute('src');
    $image[$i]['img']['srcset'] = $item->getAttribute('src');
     $i++;

}

echo "<pre>";
print_r($image);

DEMO

Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11