douti8321 2013-09-24 18:17 采纳率: 0%
浏览 20
已采纳

如何在html(来自url)中找到标签属性[关闭]

   <?php 

    $html = file_get_contents('https://vine.co/v/h5PZJxYiHra/card');
    //$videoSrc = ?;
    ?>

With function file_get_contents()

I get HTML content of URL.

I need in this html find TAG <source src="https://v.cdn.vine.co/r/videos/xxxxx.mp4 " type="video/mp4">

How to get source attribute src?

  • 写回答

2条回答 默认 最新

  • dsakktdog498483070 2013-09-24 18:24
    关注

    Theres a few ways to do this.

    1. PHP DOM - http://us1.php.net/dom

    This doo-hickey will make a XML/XHTML object given the source code you supply. It's kind of a tree like structure that you can traverse through.

    2. PHP XML - http://php.net/manual/en/book.xml.php

    Just like #1 but an older XML parser.

    3. String Literal Searching - http://php.net/manual/en/function.strpos.php

    This is an oldey but goody. Use strpos() to find the source tag start, then again to find the src tag and hopefully grab the string. It will require that the tags are perfect and doesn't leave a lot of flexibility.

    $source = strpos($html, '<source ');
    if($source!==FALSE) {
      $src_loc = strpos($html, 'src="', $source);
      if($src_loc!==FALSE) {
        $end_quote = strpos($html, '"', $src_loc + 5);
        if($end_quote!==FALSE) {
          $final_src = substr($html, $src_loc+5, $end_quote-($src_loc+5)); //TADA!
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计