bananaman Posted October 17, 2014 Share Posted October 17, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/291876-empty-arrays/ Share on other sites More sharing options...
Solution Ch0cu3r Posted October 17, 2014 Solution Share Posted October 17, 2014 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); Quote Link to comment https://forums.phpfreaks.com/topic/291876-empty-arrays/#findComment-1493973 Share on other sites More sharing options...
bananaman Posted October 17, 2014 Author Share Posted October 17, 2014 thanks it works now Quote Link to comment https://forums.phpfreaks.com/topic/291876-empty-arrays/#findComment-1493982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.