Jump to content

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

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.