0

我有一个 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";
    }
?>
4

2 回答 2

0
<?php

$output = '<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" />';

$file = 'images.html';
file_put_contents($file, $output);

?>

或者,如果您想更改现有脚本:

foreach($images as $img)
{
  echo '<img src="images/' . $img . '" />' . "\n";
  $output .= '<img src="images/' . $img . '" />' . "\n";
}

$file = 'images.html';
file_put_contents($file, $output);
于 2012-04-12T16:57:48.123 回答
0

这已经在 HTML 网页中呼应了

foreach($images as $img)
{
  echo '<img src="images/' . $img . '" />' . "\n";
}

如果您需要将其保存到文件中,请使用

$file = 'images.html';
file_put_contents($file, $output);

正如电台所说

于 2012-04-12T17:01:13.783 回答