2014-10-31 49 views
0

嗨,我需要做一个学校的任务,我需要从http://www.asaphshop.nl正则表达式的图像(和DomDoucument不工作,因为我得到多个错误。所以我需要用正则表达式。我现在唯一得到的是一个包含来自网站所有图片的长阵列,我只需要一张图片,这是我需要回显的代码部分(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

我建议使用HTML解析器这个任务 – Ghost 2014-10-31 08:13:48

+1

'DomDoucument DOES不工作,因为我得到多个错误' – 2014-10-31 08:14:26

+1

我已经看到过这种类型的问题。哦,那个问了三次以上的同一个问题的人。 – 2014-10-31 08:14:51

回答

0

我不明白的问题。你的问题在哪里... 你只是尝试使用data-src -l图片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); 
?> 
+0

解析错误:语法错误,意想不到的'('在C:\ xampp \ htdocs \ stage \ ripper2.php在线5 – Peter 2014-10-31 11:09:11

+0

是的,对不起,它只是一个字符串PHP问题 – Croises 2014-10-31 12:16:45

+0

好吧!我们现在接近!它现在回声连接,但它应该是它自己的图像 – Peter 2014-10-31 13:42:14

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; 
+1

我现在得到的是:什么都没有 – Peter 2014-10-31 08:39:17