【问题标题】:How to filter any given number of html tags through php如何通过php过滤任何给定数量的html标签
【发布时间】:2015-11-19 14:52:06
【问题描述】:

我正在做一个项目,我想随机显示任何给定数量的结果假设,我有六个 <html> 图像标签,我只想随机显示三个,这样每次我们刷新页面时随机显示任意六张图片中的任意三张

我以html代码为例

<html>
  <body>
    <div class=1>
      <a href="http://example1.com">
        <div>
          <img src="image1.jpg">
        </div>
      </a>
    </div>
    <div class=1>
      <a href="http://example2.com">
        <div>
          <img src="image2.jpg">
        </div>
      </a>
    </div>
    <div class=1>
      <a href="http://example3.com">
        <div>
          <img src="image3.jpg">
        </div>
      </a>
    </div>
    <div class=1>
      <a href="http://example4.com">
        <div>
          <img src="image4.jpg">
        </div>
      </a>
    </div>
    <div class=1>
      <a href="http://example5.com">
        <div>
          <img src="image5.jpg">
        </div>
      </a>
    </div>
    <div class=1>
      <a href="http://example6.com">
        <div>
          <img src="image6.jpg">
        </div>
      </a>
    </div>
  </body>
</html>

在这六个图像中,我只想通过 php 显示任何三个图像。有可能吗?我该怎么做? 希望您能找到更好的解决方案。 另外我想显示其他标签,如图像中的链接和更多标签,以便我可以通过 css 以更好的方式显示图像,所以我认为可以通过 switch 语句更轻松地完成

【问题讨论】:

  • 将文件名放在一个数组中。 array_rand(),然后是 foreach。或者shuffle() 然后循环3次。
  • 假设我必须用每张图片展示一些其他的东西,你能告诉我怎么做吗
  • 还有什么其他的
  • 其他标签,如图片中的链接和更多&lt;div&gt;标签,以便我可以通过css以更好的方式显示图片
  • @learner 如果您想要一个 php 代码,该代码可以获取定义数量的随机图像及其从现有页面到另一个页面的链接,请查看我的答案。我认为这就是你想要做的,希望它有所帮助。享受:)

标签: php html image dom random


【解决方案1】:

假设您有一个包含所有图像的数组。从该列表中,我们随机获取 3 张图像的键。然后我们通过一个循环回显出 img 标签:

<html>
<body>
<?php
$images = [
    'image1.jpg',
    'image2.jpg',
    'image3.jpg',
    'image4.jpg',
    'image5.jpg',
    'image6.jpg'
];

// Selects 3 random array values and returns the key for each value
$randomkeys = array_rand($images, 3);

// Here we loop through the given index keys from the $images array.
// For each key we will then get the value from $images with the index $key
foreach ($randomkeys as $key) {
    // I end with PHP_EOL (End of line) so the source code will look a bit prettier.
    echo "<div class=\"image\"><a href=\"{$images[$key]}\"><img src=\"{$images[$key]}\"></a></div>".PHP_EOL;
}
?>
</body>
</html>

如果有不清楚的地方,请告诉我

编辑 1:添加更多标签
在输出中添加更多标签并不难。如果您知道如何回显字符串和变量,您应该能够轻松添加更多标签或以您想要的方式更改它们。

正如您在更新中看到的那样,我已将 image 类添加到 ,并将链接指向与图像相同的路径,因此当您单击它时,它只会在同一窗口中打开图像。

【讨论】:

  • 您也可以只使用 array_rand() 返回的值,而不是不必要地索引到数组中。
  • 带有回声的“逗号”而不是“句点”要快很多。句点创建一个新的字符串值,逗号只是将值附加到末尾。
  • array_rand() 仅返回给定数组中的键。您没有从数组中获取值。因此,您必须对数组 @pestilence669 进行索引
  • 我已经编辑了问题,我还想显示一些其他标签,如图片中的链接和更多 &lt;div&gt; 标签,以便我可以通过 css 以更好的方式显示图片
【解决方案2】:
<?php
  $images=array('image1.jpg','image2.jpg','image3.jpg','image4.jpg','image5.jpg','image6.jpg');
shuffle($images);
for($i=0;$i<=2;$i++){
 echo '<img src="'.$images[$i].'" />';
 }
?>

【讨论】:

  • 六幅图像约束的完美表现。真的只有几十个之后才变得浪费。
  • 这个答案很完美,但它回答了OP的一半问题,请尝试包括链接(&lt;a&gt;&lt;/a&gt;
【解决方案3】:
You can keep your src paths in an array. Then you can use array_rand() function to get random array index. 

For example:
<?php

$allImages = ["images1.jpg","images2.jpg","images3.jpg","images4.jpg","images5.jpg","images6.jpg"];

//this will return random keys of $allImages array
$randomKeys = array_rand($allImages, 3);

foreach($randomKeys as $key){ ?>
  <div><a href="#"><img src="<?php echo $allImages[$key] ?> " ></a></div>
<?php
}
?>

这就是你如何使用 php 来做到这一点

【讨论】:

  • 我已经编辑了问题,我还想在图片中显示一些其他标签,如链接和更多 &lt;div&gt; 标签,以便我可以通过 css 以更好的方式显示图片
  • 好的,那么你可以添加任何你想在每个循环的 php 中添加的内容。
  • 我已经编辑了我的答案。不,你可以在divaimg 标签. In the src 中做任何你想做的事,我只是添加了 php 变量来显示随机选择的图像路径。我这解决了你的问题。请批准这个答案。祝你好运。
  • 我对所有图片使用不同的网址
【解决方案4】:

就像@AbraCadaver 所说的那样。

<?php
// vim: set ts=4 sw=4 et:

$myImages = array_map(function($i) { return "/foo$i.jpg"; }, range(1, 6));

foreach (array_map('htmlspecialchars', array_map(function ($i) { global $myImages; return $myImages[$i]; }, array_rand($myImages, 3))) as $image)
    echo "<img src=\"$image\"/>\n";

【讨论】:

    【解决方案5】:

    刚刚扩展了Morten的代码:

    <html>
    <body>
    <?php
    $images = array(
        array('img' => 'image1.jpg', 'url' => 'http://example1.com', 'div' => 'class="d1"'),
        array('img' => 'image2.jpg', 'url' => 'http://example2.com', 'div' => 'class="d2"'),
        array('img' => 'image3.jpg', 'url' => 'http://example3.com', 'div' => 'class="d3"'),
        array('img' => 'image4.jpg', 'url' => 'http://example4.com', 'div' => 'class="d4"'),
        array('img' => 'image5.jpg', 'url' => 'http://example5.com', 'div' => 'class="d5"'),
        array('img' => 'image6.jpg', 'url' => 'http://example6.com', 'div' => 'class="d6"')
    );
    
    // Selects 3 random array values and returns the key for each value
    $randomkeys = array_rand($images, 3);
    
    // Here we loop through the given index keys from the $images array.
    // For each key we will then get the value from $images with the index $key
    foreach ($randomkeys as $key) {
        // I end with PHP_EOL (End of line) so the source code will look a bit prettier.
        echo '<div class="1">' . PHP_EOL . '<a href="' . $images[$key]['url'] . '">' . PHP_EOL . '<div ' . $images[$key]['div'] . '>' . PHP_EOL . '<img src="' . $images[$key]['img'] . '">' . PHP_EOL . '</div>' . PHP_EOL . '</a></div>' . PHP_EOL;
    }
    ?>
    </body>
    </html>
    

    结果: https://3v4l.org/OAc6l

    【讨论】:

    • 为什么你使用'
      '。而不是这个。 '
      '
    • 因为他只是扩展了我的答案,@learner。我选择这种风格的原因是我可以直接使用{$images['key']},而不是通过编写echo 'something ' . $images['key'] . ' something';来连接字符串
    • @learner 为 imgs 添加 div,更改为长语法,更改结果 URL
    • @learner 我在 7 小时前和 2 天前在网上见过你,但你在这个问题上不活跃。您在等待赏金到期吗?
    【解决方案6】:

    在这种情况下,您的意思是如果页面已经准备好(可能是页面是外部网页,或者已经设计的另一个页面)我会使用Simple Html DomDownload 并将文件夹包含到项目中。

    example.html

    <html>
    <style>
    img{
        width:100px;
        height:100px;
    }
    </style>
     <body>
    <div class=1>
    <a href="http://example1.com">
    <div>                               
    <img src="image (1).jpg" >                                     
    </div>
    </a></div>
    <div class=1>
    <a href="http://example2.com">
    <div>                               
    <img src="image (2).jpg" >                                     
    </div>
    </a></div>      <div class=1>
    <a href="http://example3.com">
    <div>                               
    <img src="image (3).jpg" >                                     
    </div>
    </a></div>
    <div class=1>
    <a href="http://example4.com">
    <div>                               
    <img src="image (4).jpg" >                                     
    </div>
    </a></div>
    <div class=1>
    <a href="http://example5.com">
    <div>                               
    <img src="image (5).jpg" >                                     
    </div>
    </a></div>
    <div class=1>
    <a href="http://example6.com">
    <div>                               
    <img src="image (6).jpg" >                                     
    </div>
    </a></div>
     </body>
     </html>
    

    myphp.php

    /**echo '<style>
    img{
        width:100px;
        height:100px;
    }
    </style>';**/
    
    //using Simple HTML DOM. This file is in Simple Html Dom folder downloaded
    include('simple_html_dom.php');
    // get DOM from URL or file
    $html = file_get_html('example.html');// you can write your external website file here www.external.com/index.html
    
    // find all link
    foreach($html->find('a') as $e) 
        //echo $e->href . '<br>';
        $image_with_link['link'][]=$e->href;
    // find all image
    foreach($html->find('img') as $e)
        //echo $e->src . '<br>';
        $image_with_link['image'][]=$e->src;
    
    //for debugging
    //echo '<pre>';
    //print_r($image_with_link);
    //echo '</pre>';
    
    //loop number of times, here i want to display three images
    for($i=0;$i<3;$i++){
    //get random key from array
    $rand=array_rand($image_with_link['image'],1);
    
    //print 3 images with its links
    echo '<a href="'.$image_with_link['link'][$i].'" />';
    echo '<img src="'.$image_with_link['image'][$i].'" />';
    }
    

    【讨论】:

      【解决方案7】:

      将代码视为一个大字符串。从该字符串中删除 &lt;HTML&gt;&lt;/HTML&gt;&lt;BODY&gt;&lt;/BODY&gt;。您可以随时添加它们。接下来,分解"&lt;DIV " 周围的字符串。对于创建的数组,将"&lt;DIV " 添加到每个元素的开头。您现在有一个包含每个 div 部分的数组,其中包含您想要的每个图像和链接。然后,您可以从该数组中随机选择一个并根据需要插入:

      // remove HTML and BODY tags
          $remove1 = str_replace("<HTML>", "", $original);
          $remove2 = str_replace("<BODY>", "", $remove1);
          $remove3 = str_replace("</HTML>", "", $remove2);
          $cleaned = str_replace("</BODY>", "", $remove3);
      
      // explode remaining code around "<DIV " so we have each division for each image
          $codeArray = explode("<DIV ", $cleaned);
      
      // with our code sectioned in an array, add "<DIV " back to the start
      // of each element in the array
          for ($x = 0; $x < count($codeArray); $x++){
              $codeArray[$x] =. "<DIV ";
          }
      
      // the $codeArray now has $x elements of the sections that contain the 
      // links and images you want.
      // You can now randomly grab the div's that you want, by
      // Shuffling that array, pick the first X images you want, and then
      // echo back out the code you want.
          shuffle($codeArray);
          echo "<HTML>";
          echo "<BODY>";
          for ($x = 0; $x < $whateverNumberOfImagesYouWant; $x++){
              echo $codeArray[$x];
          }
          echo "</BODY>";
          echo "</HTML>";
      

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签