image problems with regular expressions

前端 未结 2 936
忘了有多久
忘了有多久 2020-12-22 04:45

When I run the following script, the image is not rendered well. What is the problem here? This is the code:



        
相关标签:
2条回答
  • 2020-12-22 05:33
    $pattern = "#class=\"noscript\">.*data-src-l=([\"'])(?<url>.*)\\1.*</div>#isU";
    

    But it is better to deal with the page as with the DOM structure, not as a string. \\1 is a backreference to ([\"']) so that the same quotes are used at the end of the string. Not so necessary for the URLs as there should be no direct quotes (unescaped) in them, but it is good for general purpose.

    ps: if you need everything between <img and /> (including them) - $pattern = '#class="noscript">.*(<img.*>).*</div>#isU';

    0 讨论(0)
  • 2020-12-22 05:36

    Use DOMDocument (I hope that your schoolmistress will not scold you):

    $dom = new DOMDocument();
    $dom->loadHTMLFile('http://www.asaphshop.nl/epages/asaphnl.sf/nl_NL/?ObjectPath=/Shops/asaphnl/Products/80203122');
    
    $xpath = new DOMXPath($dom);
    
    $url = $xpath->query('//div[@id="ProductImages"]/ul/li/a/img/@data-src-l')->item(0)->nodeValue;
    
    echo $url;
    
    0 讨论(0)
提交回复
热议问题