如何通过php过滤任何给定数量的html标签

lea*_*ner 11 html php random dom image

我的工作中,我要显示随机任意给定的结果数猜想的一个项目,我有六个<html>形象的标签,我只想随机显示3次,以我们每次刷新页面,它显示随机任意三个图像出任何六个

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

在这六张图片中我只想通过php显示任何三张图片.有可能,我该怎么做?希望你能找到更好的解决方案.此外,我想显示其他标签,如链接在图像和一些更多的标签,以便我可以通过CSS更好地显示图像,所以我认为它可以通过switch语句更容易完成

Mor*_*ten 5

假设你有一个包含所有图像的数组.从该列表中,我们随机获取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>
Run Code Online (Sandbox Code Playgroud)

如果有什么不清楚,请告诉我

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

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

  • `array_rand()`只返回给定数组中的键.您没有从数组中获取值.因此,您必须对数组@ pestilence669进行索引 (2认同)

Nic*_*aki 2

刚刚扩展了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>
Run Code Online (Sandbox Code Playgroud)

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