繁体   English   中英

如何在PHP数组链接内添加随机数查询字符串

[英]How To Add Random Number Query String Within PHP Array Links

目的是向PHP数组中旋转图像的末尾添加一个随机数,以防止浏览器缓存图像。 该数字应附加到我拥有数组的图像URL的末尾,并且效果很好。 我有一个随机数生成器,并且有效。 使用PHP将随机数应用于数组中的链接时,我做错了。

这是包含链接和图像的数组示例:

<?php
$content1 = '<a href="http://example.net" title="Example 1" target="_blank"><img src="http://example.com/gallery/image-1.png" alt="Image 1" width="300" height="250" class="gallery-image"></a>';
$content2 = '<a href="http://example.net" title="Example 2" target="_blank"><img src="http://example.com/gallery/image-2.png" alt="Image 2" width="300" height="250" class="gallery-image"></a>';
$content3 = '<a href="http://example.net" title="Example 3" target="_blank"><img src="http://example.com/gallery/image-3.png" alt="Image 3" width="300" height="250" class="gallery-image"></a>';
$content4 = '<a href="http://example.net" title="Example 4" target="_blank"><img src="http://example.com/gallery/image-3.png" alt="Image 4" width="300" height="250" class="gallery-image"></a>';
$content = array($content1, $content2, $content3, $content4,);
shuffle($content);
?>
<?php print $content[0] ?>

这是随机数生成器:

<?php echo rand() . "\n"; ?>

我想要的应该是这样的:

$content1 = '<a href="http://example.net" title="Example 1" target="_blank"><img src="http://example.com/gallery/image-1.png?29384756" alt="Image 1" width="300" height="250" class="gallery-image"></a>';

我试图将随机数生成器放在数组的文本字符串中,但是我做错了,因为它不会生成数字,或者HTML代码显示在HTML中,所以我不确定如何随机数将在HTML文本数组中生成。

同样,目标是在图像URL的末尾添加一个随机数查询字符串,以便数组仍显示图像并防止浏览器在刷新页面(如果有)时缓存图像。

有任何想法吗? 有更好的方法吗?

一个简单的例子:

$rand = rand();
// ...
$content2 = '<a href="http://example.net" title="Example 2" target="_blank"><img src="http://example.com/gallery/image-2.png?' . $rand .'" alt="Image 2" width="300" height="250" class="gallery-image"></a>';
// ...
$content = array($content1, $content2, $content3, $content4,);
shuffle($content);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM