Skip to content Skip to sidebar Skip to footer

Image Problems With Regular Expressions

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

Solution 1:

$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';

Solution 2:

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;

Post a Comment for "Image Problems With Regular Expressions"