doter1995 2014-10-20 06:38
浏览 7
已采纳

错误的图像,正则表达式[重复]

This question is an exact duplicate of:

I need a little bit of help. I got an assignment for school, I need to make a regular expressionscript which get an image (and later upload to the database, but that's not the problem). The real problem is that I get an array with all images from the page, but should be one image, which is: data-src-l="/WebRoot/products/8020/80203122/bilder/80203122.jpg" this is the code from the whole image:

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

This is the code with PHP:

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

UPDATE: in front of the imagelink must be the link to http://www.asaphshop.nl, so it will look into the site for the image. not inside my localhost. If you dont understand me, you can ask ;)

</div>
  • 写回答

2条回答 默认 最新

  • douji5329 2014-10-20 06:43
    关注
    (<img\s[^>]*?data-src-l\s*=\s*['\"])([^'\"]*?['\"])([^>]*?>)
    

    Try this.This will give the required img.Replace by $1http://www.asaphshop.nl$2$3.See demo.

    http://regex101.com/r/wQ1oW3/29

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单