正则表达式图像问题

彼德

嗨,我需要为学校分配作业,我需要从http://www.asaphshop.nl获取带有正则表达式的图像((并且DomDoucument不起作用,因为我遇到多个错误。因此,我需要使用Regex来完成)我现在唯一得到的是一个长数组,其中包含站点中的所有图片。我只需要一张图片,这就是我需要回显的代码的一部分(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);
?>
十字架

我不明白这个问题。问题出在哪里...您只是尝试使用data-src-l图片网址?

更改您的代码:

$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);
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章