嗨我需要为学校做作业,我需要从http://www.asaphshop.nl获得带有正则表达式的图像,(并且DomDoucument不能正常工作,因为我遇到了多个错误。所以我需要用Regex来做。我现在唯一得到的是一个包含网站所有图片的长数组。我只需要一个图像。这是我需要回应的代码的一部分(data-src-l):
<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>
到目前为止,这是我的代码:
<?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);
?>
答案 0 :(得分:0)
我不明白这个问题。你的问题在哪里...... 您只是尝试使用data-src-l image url?
更改代码:
$pattern = "/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/";
$image = ($matches[0]);
$image = str_replace('src="/', 'src="http://www.asaphshop.nl/', $image);
为:
$pattern = "/<img\s[^>]*?data-src-l="([^"]+)[^>]*?>/";
$imageLink = "http://www.asaphshop.nl". $matches[1];
$image = '<img src="'. $imageLink .'">';
并使用:
<?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);
?>
答案 1 :(得分: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;