【问题标题】:echo php output in html file在 html 文件中回显 php 输出
【发布时间】: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代码&lt;html&gt; ..stuff.. &lt;?php your_code ?&gt; ..more stuff.. &lt;/html&gt;周围的php文件中
  • 排序部分有问题吗?
  • 你能记录下你的 $images 吗?
  • 是的,解决了!我把它弄回来了,我把 php 代码放在我的 html 文件中!谢谢!

标签: php html echo


【解决方案1】:
<?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);

【讨论】:

    【解决方案2】:

    这已经在 HTML 网页中呼应了

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

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

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

    正如电台所说

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 2016-02-10
      • 2014-09-10
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多