【发布时间】:2012-04-12 16:49:01
【问题描述】:
我有一个 php 代码,它给了我以下输出:(它是一个数组)
<img src="images/image0.jpg" />
<img src="images/image1.jpg" />
<img src="images/image2.jpg" />
<img src="images/image3.jpg" />
<img src="images/image4.jpg" />
<img src="images/image5.jpg" />
<img src="images/image6.jpg" />
<img src="images/image7.jpg" />
<img src="images/image8.jpg" />
我想在我的 html 文件中打印或回显或复制或调用该确切输出。 我该怎么办?
这是我的代码 php 代码供参考:
<?php
//PHP SCRIPT: getimages.php
header('content-type: application/x-javascript');
function returnimages($dirname="./images") {
$pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"; // http://www.mkyong.com/regular-expressions/how-to-validate-image-file-extension-with-regular-expression/
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(preg_match($pattern, $file)){ //if this file is a valid image
$files[] = $file;
}
}
closedir($handle);
}
//sort($files); // http://php.net/manual/en/function.sort.php
natcasesort($files); // case insensitive "natural order" algorithm :: http://php.net/manual/en/function.natcasesort.php
return($files);
}
$images = returnimages(); //will get the array containing the images
foreach($images as $img)
{
echo '<img src="images/' . $img . '" />' . "\n";
}
?>
【问题讨论】:
-
什么不起作用?你试过什么?
-
我很困惑。你想把它放在一个html文件中吗?把你的html放在php代码
<html> ..stuff.. <?php your_code ?> ..more stuff.. </html>周围的php文件中 -
排序部分有问题吗?
-
你能记录下你的 $images 吗?
-
是的,解决了!我把它弄回来了,我把 php 代码放在我的 html 文件中!谢谢!