【问题标题】:PHP: clear the screen (Or set the screen blank)PHP:清除屏幕(或设置屏幕空白)
【发布时间】:2015-04-15 21:00:01
【问题描述】:

====================

注意:这不是重复的,因为我已经在 linked question 上尝试过解决方案,但它对我的问题不起作用

====================

如何“删除”任何以前 [浏览器] 回显的项目? - 彻底清屏(将屏幕设为空白)?

例如...:

<?php
function test($var) {
    if ($var === 0) { echo "Hello "; }
    if ($var === 1) { echo "World"; }
    if ($var < 0 || $var > 1) { [clear screen]; echo "Number is too big";}
}

test(0);
test(1);
test(666);

========更多详情========

我遇到的问题是这样的。该页面呈现 HTML 的一部分,但是当它到达关键字时,它会停止并回显我需要的内容(如果页面没有关键字,则为预期行为)。

但是,由于它不会清除屏幕和 DOM……因此,浏览器的屏幕是空白的,没有任何错误消息。这是因为我需要删除任何以前回显的输出。

在发布此问题之前,我确实尝试过ob_end_clean()。但它不起作用

PHP:

class keywords{

    private static function run(){
    ...pdo code...
    ...some more code...
    if( $sht->rowCount() === 0 ){
        ...[clear screen goes here]...
        exit("Page " . $pageID . " has no keywords");
    else...
    ...more code
    }

    ...more code    
}

在 HTML 方面,我使用的是...content="&lt;?php keywords::run(); ?&gt;" /&gt;。当页面没有关键字时,这是输出(不清除浏览器):

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <meta name="description"    content="Some page" />  
    <meta name="keywords"       content="Page: 0123456789 Page has no keywords

结果,屏幕是空白的,没有任何错误信息

【问题讨论】:

  • 终端还是浏览器?
  • @Rizier123 浏览器,但如果你都知道...
  • 删除旧的回显字符串,那么你就是你想要的,你需要的不是C/C++ clrscr()
  • 我在你的编辑中看到的是 ob_end_clean() 没有工作,但你没有解释任何事情。正如您所描述的,问题在于您正在回显项目,并且如果代码符合特定标准,则希望清除它们。如果您按照链接问题中的描述使用 ob_start() ,它应该完全符合您的要求。该问题中的其他答案也提出了更大的问题,即您不应该重复不应该存在的东西。最好先保存内容,等条件满足后再回显。

标签: php


【解决方案1】:

PHP 文档

ob_start 说:

输出缓冲区是可堆叠的,也就是说,您可以在另一个 ob_start() 处于活动状态时调用 ob_start()。只需确保调用 ob_end_flush() 的次数适当。如果多个输出回调函数处于活动状态,则会按嵌套顺序依次过滤输出。

ob_clean 说:

输出缓冲区必须由带有 PHP_OUTPUT_HANDLER_CLEANABLE 标志的 ob_start() 启动。否则 ob_clean() 将不起作用。

PHP 文档PHP_OUTPUT_HANDLER_CLEANABLE

控制是否可以清理由 ob_start() 创建的输出缓冲区。

自 PHP 5.4 起可用。

您确定可以启动/刷新/结束输出缓冲区吗?
您检查过您的服务器配置吗?
您确定没有发生输出缓冲区堆叠吗?

【讨论】:

  • 你叫它ob_start(PHP_OUTPUT_HANDLER_CLEANABLE);...更多代码...ob_clean();?
  • 两者都是正确的,另外,确保你没有嵌套 ob_start() 调用。
【解决方案2】:

您可以从output control 获得一些帮助 关键功能:ob_startob_cleanob_end_flush 试试下面的代码

<?php
ob_start();
function test($var) {
    if ($var === 0) { echo "Hello "; }
    if ($var === 1) { echo "World"; }
    if ($var < 0 || $var > 1) { 
        ob_clean(); 
        echo "Number is too big";
    }
}

test(0);
test(1);
test(666);
ob_end_flush();

【讨论】:

    【解决方案3】:

    您不能执行以下操作是否有原因?在您知道要打印/回显的内容之前,您不应该输出

    <?php
    $output = "";
    function test($var) {
    if ($var === 0) { $output = "Hello "; }
    if ($var === 1) { $output = "World"; }
    if ($var < 0 || $var > 1) { [clear screen]; $output="Number is       too big";}
    }
    Function outputCheck(){
      Echo $output;
    }
    
    test(0);
    test(1);
    test(666);
    
    outputCheck();
    

    【讨论】:

      【解决方案4】:

      您不能删除输出。 通常服务器会一次发送每个输出。所以打印或回声不能被反转。但是您可以通过使用 ob_start();

      启动所有代码来缓冲输出

      在代码结束时,您可以使用 ob_end_flush();

      将缓冲区发送到客户端

      要输出错误详细信息,您可以使用自定义函数,例如:

      function error($str) {
          ob_end_clean();
          exit($str);
      }
      

      这样就可以了。

      但是

      在发送 html 之前将所有信息收集到您的元数据会好得多。这将导致更快地发送到客户端。如果所有信息都清楚,您只需输出元数据:

      $test1 = test(1);
      $test2 = test(2);
      if //all my content is okay {
          ...paste html here...
      }
      

      【讨论】:

        【解决方案5】:

        您以非常糟糕的方式使用“退出”: http://php.net/exit

        Exit 将完成您的脚本的执行,我想这不是您所期望的行为。因此,当文档没有关键字时,您的代码无法正常工作:

        class keywords{
        
            private static function run(){
            ...pdo code...
            ...some more code...
            if( $sht->rowCount() === 0 ){
                ...[clear screen goes here]...
                exit("Page " . $pageID . " has no keywords");
            else...
            ...more code
            }
        
            ...more code    
        }
        

        您必须使用“return”或其他方式退出“运行”但不结束 PHP 脚本执行!

        例如(将此文本留在关键字标签中):

        class keywords{
        
            private static function run(){
            ...pdo code...
            ...some more code...
            if( $sht->rowCount() === 0 ){
                ...[clear screen goes here]...
                echo "Page " . $pageID . " has no keywords";
                return;
            else...
            ...more code
            }
        
            ...more code    
        }
        

        示例(不留任何文字):

        class keywords{
        
            private static function run(){
            ...pdo code...
            ...some more code...
            if( $sht->rowCount() === 0 ){
                ...[clear screen goes here]...
                return "Page " . $pageID . " has no keywords";
            else...
            ...more code
            }
        
            ...more code    
        }
        

        该文本可用于警告调用者该问题,但在您的代码中,它会默默地将关键字标签留空,但仍让 PHP 脚本的其余部分运行。

        最好的问候。

        【讨论】:

          【解决方案6】:

          由于我不了解您的需求,实际上您并不想清除屏幕,而是在浏览器中看到错误消息,而无需查看源代码。这是不可能的,因为你已经打开了标签。

          试试这个,而不是你的退出代码:

             exit('<html><head></head><body>Page ' . $pageID . " has no keywords</body></html>");
          

          您必须关闭您的元标记和标题并开始正文标记。

          【讨论】:

            【解决方案7】:
            <?php
            ob_start(); //starts the buffer
            function test($var) {
                if ($var === 0) { echo "Hello "; }
                if ($var === 1) { echo "World"; }
                if ($var < 0 || $var > 1)
                {
                     ob_get_clean(); //fetches buffer (not used here) and cleans it. It do not stop the buffer from working like  ob_end_clean();
                     echo "Number is too big";
                }
            }
            
            test(0);
            test(1);
            test(666);
            

            这通常用于加载自定义插件的 cms'es。人们倾向于在文件末尾添加前导输入或在第一个 &lt;?php 标记之前添加空格/输入,因此引擎通常打开 ob 处理程序,然后在加载所有内容之后(以及在模板用于输出之前 - 它从所有错误,输入等等。

            【讨论】:

              【解决方案8】:

              好的...你应该使用这个:

              exit(' " /> </head><body> Page '.$pageID.' has no keywords </body></html>');
              

              【讨论】:

              • 我已经在 5 天前写过相同的解决方案(请参阅下方或上方的我的 cmets)...在发布之前尝试阅读其他答案...但是您是对的,这是解决此类问题的方法...问题...
              • 对不起亚当,我没看到。我刚刚给了你一个 +1
              • 至少我很高兴看到有人阅读了这个问题 ;-)
              【解决方案9】:

              好的,我会尝试以多种方式面对这个问题。我们可以理解,您的第一个“测试”脚本:

              <?php
              function test($var) {
                  if ($var === 0) {
                      echo "Hello ";
                  }
                  if ($var === 1) {
                      echo "World";
                  }
                  if ($var < 0 || $var > 1) {
                      echo "Number is too big";
                  }
              }
              
              test(0);
              test(1);
              test(666);
              

              应该只导致:

              数字太大

              正如您在其他答案中看到的 - ob_start 适合于此,但由于它用于函数,因此必须正确嵌套。下面的例子可以正常工作。

              function test($var)
              {
                  if ($var === 0) {
                      echo "Hello ";
                  }
                  if ($var === 1) {
                      echo "World";
                  }
                  if ($var < 0 || $var > 1) {
                      ob_clean();//clear all output till now
                      echo "Number is too big";
                  }
              }
              ob_start();//start buffering output
              test(0);
              test(1);
              test(666);
              

              所以我们掌握了基础知识。让我们回到您的代码。

              我假设 HTML 是这样的:

              <!DOCTYPE html>
              <html>
              <head>
                  <meta charset="UTF-8">
                  <meta name="description"    content="Some page" />  
                  <meta name="keywords"       content="<?php keywords::run(); ?>" />
              </head>
              <body>
              maybe some content?...
              </body>
              </html>
              

              不清楚您是想发送只有错误的空白页,还是将此错误放在您的正文中。我假设后者。

              可能的解决方案:在您的内容中的某处放置额外的错误

              因此,我们希望从关键字::run() 中捕获该错误并将其放置在正文中的某个位置。让我们为此使用新类!

              class YourErrorCatcher
              {
              
                  private static $errors = array();//array for collecting all your errors
              
                  public static function addError($errorString)//method to add new error
                  {
                      self::$errors[] = $errorString;
                  }
              
                  public static function echoErrors()//function echoing all your errors in selected place
                  {
                      $errorMessage = implode("<br>", self::$errors);
                      echo $errorMessage;
                      return true;
                  }
              
              }
              

              现在你的 HTML 看起来和以前一样了,只是增加了一点:

              <body>
              maybe some content?...
              and here we have our errors:
              <?php YourErrorCatcher::echoErrors(); ?>
              </body>
              

              现在您的关键字类应该如何看待?

              class keywords{
              
                  private static function run(){
                  //i assume that this function is echoing some keywords, so let's start buffering output
                  ob_start();
                  ...pdo code...
                  ...some more code...
                  if( $sht->rowCount() === 0 ){
                  ob_clean();//there's error - clear buffer!
                  //"throw" error
                  YourErrorCatcher::addError("Page " . $pageID . " has no keywords");
                  YourErrorCatcher::addError('Yes, you can "throw" more than one error in this solution and all of them will be shown.');
                  //instead of exit we're returning false
                  return false;
                  else...
                  ...more code
                  }
              
                  ...more code    
              }
              

              这个开了很久了,也许对你有帮助。如果这对您有帮助,请告诉我!

              【讨论】:

                【解决方案10】:

                我尝试了您在描述中提供的示例代码,下面的代码对我有用。

                <?php
                    ob_start();
                    function test($var) {
                        if ($var === 0) { echo "Hello "; }
                        if ($var === 1) { echo "World"; }
                        if ($var < 0 || $var > 1) { 
                            ob_end_clean(); 
                            echo "Number is too big";
                        }
                    }
                
                    test(0);
                    test(1);
                    test(666);
                

                更多详情请关注ob_end

                让我知道它是否对你有用。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2015-10-03
                  • 2018-09-18
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-03-18
                  相关资源
                  最近更新 更多