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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题