在html文件中回显php输出

时间:2012-04-12 16:49:02

标签: php html echo

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

这已经将它回显到HTML网页

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

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

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

正如电台所述