Jump to content

bananaman

Recommended Posts

Hi, please help me :(

for school i need to make an assignment and i need to make an script with regular expressions to get an image from a site named asaphshop.nl. but the only thing happens is that it's searching in the localhost what should be on the site.

this is my code:

​ <?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 = '#class="noscript">.*(<img.*>).*</div>#isU';
preg_match_all($pattern, $htmlcode, $matches);
//print_r ($matches);
$image = ($matches[0]);
print_r ($image);
?>

 in the attached file is the thing i get

post-173230-0-48135100-1413541478_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/291876-empty-arrays/
Share on other sites

but the only thing happens is that it's searching in the localhost what should be on the site.

You mean to say the images are being loaded from localhost and not asaphshop.nl

 

This is because the urls to the images does not include the url to asaphshop.nl. The urls to the images look like this   /WebRoot/AsaphNL/Shops/.../80203122_xs.jpg  (shortened). They all begin with a forward slash. When a url starts with a / it means the root of the current url, which in your case it'll be localhost.

 

In order for the images to load from asaphshop.nl you need to replace the / at the start of each image url with http://asaphshop.nl/

 

Example code

$image = $matches[0];

$image = str_replace('src="/', 'src="http://asaphshop.nl/', $image);

echo $image; // image should be loaded from asaphshop.nl

Alternatively use array_map to perform the operation for all images in the array

$matches = array_map( function($item) { 
                          return str_replace('src="/', 'src="http://asaphshop.nl/', $item); 
                      }, $matches);

print_r ($matches);
Link to comment
https://forums.phpfreaks.com/topic/291876-empty-arrays/#findComment-1493973
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.