如何使用php覆盖整个页面的新内容?

时间:2014-05-28 08:40:11

标签: php overwrite

我想清除其他内容,并用不同的内容重写整个页面。

我有:

网主页:

<?php
    include("script.php");
?>
//blah blah, some pre-coded content

的script.php

<?php
if(preg_match('/(?i)msie [1-8]/',$_SERVER['HTTP_USER_AGENT']))
{
    // code to replace everything that is pre-coded in the mainpage
    // and include the new content
    include("replacement.php");
}
?>

我的问题是,如何删除所有以前的内容以覆盖php中的新内容?

我正在寻找类似的东西(但使用PHP):

Javascript:
<script>
document.body.innerHTML = "";
</script>

总结一下:

我在寻找什么?:如何清除页面中的所有内容,并重写新内容。使用 PHP

3 个答案:

答案 0 :(得分:3)

如果我理解正确,你需要删除php生成的输出:

ob_start();         //Start buffering
echo "Old content, will not be shown" ;
ob_end_clean();     //Erase the buffer

echo "New content, will be shown" ;

答案 1 :(得分:0)

您无法清除输出,因为您将其直接发送到网络服务器,而网络服务器可能已将其发送给客户。

因此,您应该只输出一些东西,而不是覆盖,而您实际上想要输出。您可以通过简单的ìfelse逻辑来实现此目标。

另请参阅输出缓存:

http://www.php.net/manual/de/function.ob-start.php

答案 2 :(得分:0)

我想这就是你想要的

ob_start();
echo "Hello world";
if(preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT'])){
    ob_clean();
    echo "Hello Kitty";
}
ob_get_contents();

但是,在我看来,最好根据浏览器进行切换,而不是使用输出缓冲ID,这是不必要的。