2014-10-16 93 views
-1

在下面的例子中,我的數組返回空。我需要將(完整!)圖像鏈接放入我的數組中。Array returns empty

這是我的代碼:

<?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\"\>(.*?)\<\/div\>/imU"; 
    preg_match_all($pattern, $htmlcode, $matches); 
    //print_r ($matches); 
    $image = ($matches[0]); 
    print_r ($image); 
?> 

這是我使用的嘗試,並取得(完成!)圖片鏈接到我的數組文件的一部分:

<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> 
+2

你應該** ** NEVER解析HTML用正則表達式。 – zessx 2014-10-16 10:02:02

+0

爲什麼不呢?我不明白。 – bananaman 2014-10-16 10:04:15

回答

0

第一:你不應該用正則表達式解析HTML,因爲HTML太複雜,不能像那樣解析。看看this post

對於你的問題,你的問題是你使用多行修飾符而不是單行修飾符。改變你的正則表達式爲這一個:

/class=\"noscript\"\>(.*?)\<\/div\>/isU 

如果你想要一個乾淨的結果,用這個(與回顧後/超前):

/(?<=class=\"noscript\"\>)(.*?)(?=\<\/div\>)/isU 
+0

謝謝,但現在我的數組仍然不好,這是現在的鏈接:http://localhost/WebRoot/products/8020/80203122/bilder/80203122.jpg 但它需要退出本地主機,如下所示: http://www.asaphshop.nl/WebRoot/products/8020/80203122/bilder/80203122.jpg我該怎麼做? – bananaman 2014-10-16 10:17:49

+0

這是另一個問題。你應該用'src =「http:// www.asaphshop.nl /'來替換每一個'src =」/'。相同的,'data-src-xs =「/','href =」/'... – zessx 2014-10-16 10:31:13