图像链接问题正则表达式

时间:2014-10-16 08:28:16

标签: php regex

当我运行以下脚本时,图像渲染效果不佳。这里有什么问题?这是代码(这是学校的一项任务,需要用正则表达式来完成......):

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

这是我需要复制的链接部分(data-src-l部分),前面有http://www.asaphshop.nl的链接,所以我有一个(完整的)图像链接:

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

1 个答案:

答案 0 :(得分:0)

使用模式data-src-l="(.*)",它应该

请参阅demo here

正则表达式与文字data-src-l="匹配,然后捕获任何(.*),直到最后一个双引号"

更好的匹配是[^"]*而不是.*来捕捉除"之外的所有内容([]是一类字符,以^开头反转它(除了跟随字符之外的所有字符),"代表不想要的东西。

在演示中,您可以使用它,并在右侧面板上查看说明。