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

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作