PHP主要初学者!!我想从目录中获取所有图像,排除任何非.jpg文件,随机播放结果然后显示字符串。最终它将合并为某种幻灯片。一切都很顺利,直到我试图改变结果。我没有输出,只是一个空白的屏幕。
<?php
$rootpath = 'images/slide/';
$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootpath));
foreach($fileinfos as $pathnames => $fileinfo) {
shuffle($pathnames);
foreach ($pathnames as $pathname) {
if (preg_match("/^.*\.(lck|bak|swf|mno|png|php)$/i", $pathname)) {
} else {
echo $pathname. "<br />";
}
}
}
&GT;
解!!感谢所有的帮助,让自己的事情变得更加困难。
<?php
$rootpath = 'images/slide/';
$pathnames = scandir($rootpath);
shuffle($pathnames);
foreach ($pathnames as $pathname) {
if (preg_match("/^.*\.(lck|bak|swf|mno|png|php)$/i", $pathname)) {
} else {
print_r($pathname);
}
}
&GT;
答案 0 :(得分:1)
如果你说的$ pathnames确实是一个字符串,那就完美了。访问http://writecodeonline.com/php/,将下面的代码粘贴到textarea中,然后多次点击“运行代码”。注意它们是如何被正确随机化的。
还有其他问题......
$pathnames = "images/slide/IMG_2747.JPG images/slide/100_0547.JPG images/slide/IMG_6039.JPG images/slide/IMG_2188.JPG images/slide/IMG_1114.JPG images/slide/IMG_2135.JPG images/slide/IMG_8990.JPG images/slide/DSCN4634.JPG images/slide/IMG_0739.JPG images/slide/IMG_5145.JPG";
$splitpathnames = explode(" ", $pathnames);
shuffle($splitpathnames);
foreach ($splitpathnames as $pathname) {
if (!preg_match("/^.*\.(lck|bak|swf|mno|png|php)$/i", $pathname)) {
echo $pathname. "<br />";
}
}
答案 1 :(得分:0)
根据您的评论,$ pathnames不是数组。您必须首先根据字符串生成一个数组,然后将其洗牌:
$pathnamesArray = explode(" ", $pathnames);
shuffle($pathnamesArray);
foreach ($pathnamesArray as $pathname) {
if (!preg_match("/^.*\.(lck|bak|swf|mno|png|php)$/i", $pathname)) {
echo $pathname;
}
}
答案 2 :(得分:0)
您可以对对象的数组表示进行排序,但不能对象本身进行排序,因为它没有属性顺序。试试这个吗?
<?php
$rootpath = 'images/slide/';
$fileinfos = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootpath));
foreach($fileinfos as $pathnames => $fileinfo) {
if (!$fileinfo->isFile()) continue;
if (preg_match("/^.*\.(lck|bak|swf|mno|png|php)$/i", $pathnames)) {
} else {
$arr = (array)$pathnames;
shuffle($arr);
foreach($arr as $pics){
print "<img src = " . $pics . " />";
}
}
}
?>