duanlu1950 2014-10-31 08:12
浏览 17
已采纳

正则表达式图像问题

Hi i need to make an assignment for school where i need to get an image with Regular expressions from http://www.asaphshop.nl, (and DomDoucument DOES NOT work because i get multiple errors. So i need to do it with Regex. The only thing i get now is a long array with all pictures from the site. I only need one image. This is the part (the piece of data-src-l)of the code i need to echo:

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

This is my code so far:

<?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);
?>
  • 写回答

2条回答 默认 最新

  • duancan2539 2014-10-31 08:32
    关注

    I don't understand the question. Where is your problem... You just try to use the data-src-l image url ?

    change in your code:

    $pattern = "/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/";
    $image = ($matches[0]);
    $image = str_replace('src="/', 'src="http://www.asaphshop.nl/', $image);
    

    to:

    $pattern = "/<img\s[^>]*?data-src-l="([^"]+)[^>]*?>/";
    $imageLink = "http://www.asaphshop.nl". $matches[1];
    $image = '<img src="'. $imageLink .'">';
    

    and use:

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)