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

时间:2016-03-20 19:44:10

标签: php arrays string url random

目标是在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>';

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

同样,目标是在图像URL的末尾添加一个随机数查询字符串,这样数组仍会显示图像并阻止浏览器在/刷新页面时缓存图像。

有什么想法吗?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

一个简单的例子:

bins = np.arange(np.min(rand_array), np.max(rand_array)+2) 
# [0 1 2 3]
counts, _ = np.histogram(rand_array, bins=bins)
print(counts) # [130 145 125]