PHP 将 HTML 表行写入网页


PHP Write HTML Table Row to Webpage

我正在尝试逐行将HTML表格写入网页,而不是在整个页面处理完毕后将其全部显示出来。

我已经阅读并尝试添加ob_flush()flush()ob_start(); ob_implicit_flush(true); ob_end_flush();

但是我尝试的所有内容都导致整个表同时出现,所以我不确定是否可能是代码放错位置、对使用的误解或服务器上的设置。

ob_start();
$url = "http://www.example.com";
$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
echo "<table>
<th>Path</th>
<th>Alt</th>
<th>Height</th>
<th>Width</th>";
foreach ($tags as $tag){
    $image = $tag->getAttribute('src');
    $alt = $tag->getAttribute('alt');
    $height = $tag->getAttribute('height');
    $width = $tag->getAttribute('width');
    echo "<tr>
    <td>$image</td>
    <td>$alt</td>
    <td>$height</td>
    <td>$width</td>
    </tr>";
    ob_flush();
    flush();
}
echo "</table><br>";
刷新

可能会被您正在使用的 Web 服务器中断。最常见的是打开GZIP将导致输出首先完成,然后再以压缩格式发送整个内容。它也可能是服务器本身,例如一些较旧的Windows服务器。

严格来说,您不需要代码的输出缓冲区部分。对于你正在做的事情,它是不需要的。同花顺() 应该就足够了。

如果您在本教程中跳到"使用 gzip",您可以找到解决问题的方法。(感谢詹贝图奇)

例如...

<?php 
ob_implicit_flush(true);
$buffer = "<br>";
echo "see this immediately.<br>";
echo $buffer;
ob_flush();
sleep(5);
echo "some time has passed";
?>

谢谢