【问题标题】:Getting image links from HTML从 HTML 中获取图片链接
【发布时间】:2011-12-17 10:43:12
【问题描述】:

我有以下链接:

<a href="http://example.com/src/abc.png"><img src="http://example.com/res/bca.png"></a>
<a href="http://example.com/src/hvc.gif"><img src="http://example.com/res/ncq.jpg"></a>

使用 PHP,我希望能够在图像 URL 中获取仅包含 src 的链接,并且这些链接只能是图像(png、gif、jpg 等)。我遇到的问题是我不知道文件的内容,但确定它包含看起来像上面的链接;如,我不知道图像的文件名。

简而言之,有没有办法使用 PHP 获取所有链接(仅文件名中包含 src 的链接),并将它们放入数组或字符串中?我已经将页面的来源(包含图像链接)作为 $html。

任何帮助将不胜感激。

【问题讨论】:

    标签: php html arrays regex


    【解决方案1】:

    以下链接对你有用

    来自第二个链接的正则表达式解决方案(我稍微编辑了一下):

    function linkExtractor($html){
     $linkArray = array();
     if(preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i',$html,$matches,PREG_SET_ORDER)){
      foreach($matches as $match){
       array_push($linkArray,array($match[1],$match[2]));
      }
     }
     return $linkArray;
    }
    

    【讨论】:

    • 这似乎可行,但我在想也许只将以http://images. 开头的字符串放在数组中?我不想要所有的图像。有什么想法吗?
    • @user1015599 您可以更改正则表达式以包含它。它可能看起来像这样:'/&lt;img\s+.*?src=[\"\']http\:\/\/images\.?([^\"\' &gt;]*)[\"\']?[^&gt;]*&gt;/i'
    【解决方案2】:

    你尝试过这样的事情吗?

    $regexp = "<img[^']*?src=\"([^']*?)\"[^']*?>";
    
    if(preg_match_all("/$regexp/siU", $input, $matches)) {
      echo "<pre>";
      print_r($matches);
      echo "</pre>";
    }
    

    不过,您可能应该使用 SimpleHTMLDOM 之类的东西。

    【讨论】:

      【解决方案3】:

      如果您不想使用外部库,可以使用内置 DOM 选项 PHP 链接:http://www.php.net/manual/en/book.dom.php

      示例代码

      <?php
      
      //string is the (x)html document
      $links = array();
      $string = '<html><body><a href="http://xyz.com/src/abc.png"><img src="http://xyz.com/res/bca.png"></a><a href="http://xyz.com/src/hvc.gif"><img src="http://xyz.com/res/ncq.jpg"></a></body></html>';
      
      //Load/parse the (x)html document
      $doc = new DOMDocument();
      $doc->loadHTML($string);
      
      //get all 'a' elements (links)
      $elements = $doc->getElementsByTagName('a');
      
      //Now check if we got results
      if($elements->length >= 1)
      {
         //We got results, check each result
         foreach($elements as $element)
         {
            //Check if this Link has an img child element
            $img = $element->getElementsByTagName('img');
            //You can validate if the src contains .jpg extension if you want
            //but for this example I'm skipping this
            if($img->length == 1)
            {
               //We got an link that has a img child element, store link
               $links[] = $element->getAttribute('href');
            }
         }
      
         //show all links
         echo '<pre>'."\r\n";
         print_r($links);
         echo '</pre>'."\r\n";
      
      }
      ?>
      

      【讨论】:

        【解决方案4】:

        你应该试试 dom 文档。

        <?php
        
        @$dom = new DOMDocument();
        @$dom->loadHTML($html); // $html is HTML content
        $dom->preserveWhiteSpace = false;
        
        $tags_img = $dom->getElementsByTagName('img');
        
        $images = array();
        
        foreach($tags_img as $img)
        {
            $images[] = $img->getAttribute('src');  
        }
        
        echo '<pre>';
        print_r($images);
        exit;
        
        ?>
        

        此外,您还可以像仅来自 xyz.com 的图像一样进行域检查

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-10
          • 1970-01-01
          • 2010-10-25
          • 2022-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多