dongzhi1949 2014-05-17 16:58 采纳率: 100%
浏览 312
已采纳

如何在php中获取图像alt =“...”的值?

hi all i got many sets of data show below(in $code variable). i wonder how i can output all values of image alt="..." ? For example i want to get : Music Club ( sun ) Music 09

from:

        <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />

sample set of data:

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
        <div class="portfolio-item">
            <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
                <span class="portfolio-item-hover"></span>
                <span class="fullscreen"><i class="icon-play"></i></span>
                <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
            </a>
            <div class="portfolio-item-title">
                <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
  • 写回答

3条回答

  • doujie4050 2014-05-17 17:12
    关注

    If you realy want to use preg_match_all you could use the following code.

    <?php
    $str = <<<END
    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
            <div class="portfolio-item">
                <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
                    <span class="portfolio-item-hover"></span>
                    <span class="fullscreen"><i class="icon-play"></i></span>
                    <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
                </a>
                <div class="portfolio-item-title">
                    <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
    END;
    
    preg_match_all('/<img(.*?)alt=\"(.*?)\"(.*?)>/si', $str, $out, PREG_SET_ORDER);
    
    //see first output
    var_dump($out);
    
    /*
    array(1) {
      [0]=>
      array(4) {
        [0]=>
        string(105) "<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />"
        [1]=>
        string(65) " src="http://www.example.com/teststorage/episodes/11224/201.jpg" "
        [2]=>
        string(27) "Music Club ( sun ) Music 09"
        [3]=>
        string(2) " /"
      }
    }
    */
    
    //clean array
    $alt = array();
    
    foreach($out as $val) {
      $alt[] =  $val[2];
    }
    
    //see cleaned output
    var_dump($alt);
    /*
    array(1) {
      [0]=>
      string(27) "Music Club ( sun ) Music 09"
    }
    */
    ?>
    

    If you want to do the right thing however, I would look into simple_html_dom. You can do something like:

    <?php
    // Create DOM from URL or file
    $html = file_get_html('http://www.example.com/page_i_want_to_spider.php');
    
    // Find all images
    foreach($html->find('img') as $element)
           echo $element->alt . '<br>';
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab