0

Hi i need to make an assignment for school where i need to get an image with Regular expressions from http://www.asaphshop.nl, (and DomDoucument DOES NOT work because i get multiple errors. So i need to do it with Regex. The only thing i get now is a long array with all pictures from the site. I only need one image. This is the part (the piece of data-src-l)of the code i need to echo:

<div id="ProductImages" class="noscript">
    <ul>

      <li>
        <a href="/WebRoot/products/8020/80203122/bilder/80203122.jpg">
          <img
           itemprop="image"
           alt="Jesus Remember Me - Taize Songs (2CD)"
           src="/WebRoot/AsaphNL/Shops/asaphnl/5422/8F43/62EE/D698/EF8E/4DEB/AED5/3B0E/80203122_xs.jpg"
           data-src-xs="/WebRoot/AsaphNL/Shops/asaphnl/5422/8F43/62EE/D698/EF8E/4DEB/AED5/3B0E/80203122_xs.jpg"
           data-src-s="/WebRoot/products/8020/80203122/bilder/80203122_s.jpg"

           data-src-m="/WebRoot/products/8020/80203122/bilder/80203122_m.jpg"

           data-src-l="/WebRoot/products/8020/80203122/bilder/80203122.jpg"
         />
        </a>
      </li>


    </ul>
  </div>

This is my code so far:

<?php
header('Content-Type: text/html; charset=utf-8');
$url = "http://www.asaphshop.nl/epages/asaphnl.sf/nl_NL/?ObjectPath=/Shops/asaphnl/Products/80203122";
$htmlcode = file_get_contents($url);
$pattern = "/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/";
preg_match_all($pattern, $htmlcode, $matches);
//print_r ($matches);
$image = ($matches[0]);
$image = str_replace('src="/', 'src="http://www.asaphshop.nl/', $image);
print_r ($image);
?>
Peter
  • 1
  • 5

2 Answers2

0

I don't understand the question. Where is your problem... You just try to use the data-src-l image url ?

change in your code:

$pattern = "/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/";
$image = ($matches[0]);
$image = str_replace('src="/', 'src="http://www.asaphshop.nl/', $image);

to:

$pattern = "/<img\s[^>]*?data-src-l="([^"]+)[^>]*?>/";
$imageLink = "http://www.asaphshop.nl". $matches[1];
$image = '<img src="'. $imageLink .'">';

and use:

<?php
header('Content-Type: text/html; charset=utf-8');
$url = "http://www.asaphshop.nl/epages/asaphnl.sf/nl_NL/?ObjectPath=/Shops/asaphnl/Products/80203122";
$htmlcode = file_get_contents($url);
$pattern = '/<img\s[^>]*?data-src-l="([^"]+)[^>]*?>/';
preg_match($pattern, $htmlcode, $matches);
$imageLink = "http://www.asaphshop.nl". $matches[1];
$image = '<img src="'. $imageLink .'">';
print_r ($image);
?>
Croises
  • 18,570
  • 4
  • 30
  • 47
0
$url = "http://www.asaphshop.nl/epages/asaphnl.sf/nl_NL/?ObjectPath=/Shops/asaphnl/Products/80203122";
$htmlcode = file_get_contents($url);
preg_match('/data-src-l="(.*)"/',$htmlcode ,$matches);
$image = ($matches[1]);
$path= 'http://www.asaphshop.nl'.$image;
Amit Kumar Sahu
  • 495
  • 6
  • 15