-5

How do I copy the url for the photo from the site if the url written in styles.

<div class="thumb">
               <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover">

<span class="thumb-overlay"></span></a>
 </div>
Р. Р.
  • 1
  • 1

2 Answers2

2

You can use DOMDocument and DOMXPath

$html = <<< EOF
<div class="thumb">
               <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover">

<span class="thumb-overlay"></span></a>
 </div>
EOF;

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

foreach ($xpath->query('//div[contains(@class,"thumb")]/a') as $item) {

    $img_src =  $item->getAttribute('style');
    $img_src = preg_replace('/.*?\((.*?)\).*?/', '$1', $img_src);
    print $img_src;
}

# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

You can use regex to extract only background image or background:url format

$str=' 
<div class="thumb">
               <a href="http://goruzont.blogspot.com/2017/04/blog-post_6440.html" style="background:url(https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg) no-repeat center center;background-size:cover">

<span class="thumb-overlay"></span></a>
 </div>'; 

preg_match_all('~\bbackground(-image)?\s*:(.*?)\(\s*(\'|")?(?<image>.*?)\3?\s*\)~i',$str,$matches);
$images = $matches['image'];
foreach($images as $img){
     echo $img;
}
# https://1.bp.blogspot.com/-6vpIH5iqPYs/WPzlNdxsRpI/AAAAAAAAntU/d7U_Ch_6FiIPwosNL4tWwqBeXw8qwo2nACLcB/s1600/1424051.jpg 
Sinan Ulker
  • 445
  • 8
  • 20