【问题标题】:HTML output from PHP functionPHP 函数的 HTML 输出
【发布时间】:2010-10-01 20:48:20
【问题描述】:

我有一个函数,其目的是返回一些 HTML ($offer_html) 以显示在我的页面上。但是,我看到了一些奇怪的东西。

在下面,您会看到一个 div 首先添加到 $offer_html(DIV 在最后关闭)。

$offer_html = '<div class="box middle offer alignleft">';
$offer_html .= '<p class="detail alignleft">' . $volum . '</p>';
$offer_html .= '<p class="detail alignleft">' . $produsent . '</p>';
$offer_html .= wpfp_link();
$offer_html .= '</div';

return $offer_html;

我的问题在于函数 wpfp_link()。该函数返回 HTML,但这个 HTML 最终完全放错了位置。当页面呈现时,HTML 如下所示:

<img class="wpfp-img" title="Favorite" alt="Favorite" src="http://localhost:8888/wordpress/wp-content/plugins/wp-favorite-posts/img/remove.png">
<div class="box middle offer alignleft">
</div>

如您所见,wpfp_link() 返回的 HTML 最终位于我希望它位于其中的 DIV 之外。

有人知道为什么会这样吗?

【问题讨论】:

  • 您可能会发现Wordpress Stack Exchange 站点对这个问题更有用。或者,至少,用“Wordpress”标记它。
  • &lt;p class="detail alignleft"&gt; 也不见了,否则我会说wpfp_link() 不返回任何内容,而是直接打印。
  • 如果我像这样回显它 "$offer_html .= echo wpfp_link();"该页面生成错误。这是我的语法吗?

标签: php html wordpress


【解决方案1】:

您的 wpfp_link() 不返回 HTML 字符串,它直接回显该字符串。您必须从输出缓冲区中捕获它,如下所示:

<?php
// Turn on outbut buffering
ob_start();


function your_function()
{
    // Fill outbut buffer
    wpfp_link();

    // Fetch and clean output buffer
    $wpfp_html = ob_get_clean();

    $offer_html = '<div class="box middle offer alignleft">';
    $offer_html .= '<p class="detail alignleft">' . $volum . '</p>';
    $offer_html .= '<p class="detail alignleft">' . $produsent . '</p>';

    // Use stuff from output buffer instead of wpfp_link() method.
    $offer_html .= $wpfp_html;

    $offer_html .= '</div';

    return $offer_html;
}

// Don't forget to handle active output buffering!
ob_end_flush();
?>

【讨论】:

  • 你应该把 ob_start 和 ob_end_flush 放在函数中,因为它只需要在那个范围内。
  • ob_start 和 ob_end_flush 在方法范围之外应该表明这些方法可能会影响其他代码部分!您应该确保没有其他活动的输出缓冲,或者您应该相应地处理它!
  • 请考虑将 kemps 回答作为要走的路。输出缓冲作为一种变通方法是邪恶的,当您被提供传递一个使函数返回其值而不是回显它的参数时。
【解决方案2】:

还有另一种 - 我相信更好的 - 方法可以解决这个问题。查看包含该函数 (WP Favorite Posts) 的插件的源代码,您可以看到 wpfp_link() 函数接受各种参数,其中第一个是一个标志,在打印输出和返回它之间改变其行为。

function wpfp_link( $return = 0, $action = "", $show_span = 1, $args = array() ) {
    global $post;
    $post_id = $post->ID;
    extract($args);
    if ($show_span)
        $str = "<span class='wpfp-span'>";
    $str .= wpfp_before_link_img();
    $str .= wpfp_loading_img();
    $wpfp_options = wpfp_get_options();
    if ($action == "remove"):
        $str .= wpfp_link_html($post_id, wpfp_get_option('remove_favorite'), "remove");
    elseif ($action == "add"):
        $str .= wpfp_link_html($post_id, wpfp_get_option('add_favorite'), "add");
    elseif (wpfp_check_favorited($post_id)):
        $str .= wpfp_link_html($post_id, wpfp_get_option('remove_favorite'), "remove");
    else:
        $str .= wpfp_link_html($post_id, wpfp_get_option('add_favorite'), "add");
    endif;
    if ($show_span)
        $str .= "</span>";
    if ($return) { return $str; } else { echo $str; }
}

所以

wpfp_link(TRUE);

应该按照你的预期行事。

我检查了源代码,但这可能记录在插件信息的某个地方。

【讨论】:

  • 感谢您加倍努力!
【解决方案3】:

不可能从您显示的小代码中确定,但也许您需要直接echo 字符串而不是返回它。

【讨论】:

    【解决方案4】:

    wpfp_link 函数可能是在回显 HTML 而不是返回它。您可以使用输出缓冲将 HTML 作为字符串获取。

    $offer_html = '<div class="box middle offer alignleft">';
    $offer_html .= '<p class="detail alignleft">' . $volum . '</p>';
    $offer_html .= '<p class="detail alignleft">' . $produsent . '</p>';
    ob_start();
    wpfp_link();
    $offer_html .= ob_get_clean();
    $offer_html .= '</div';
    
    return $offer_html;
    

    【讨论】:

      【解决方案5】:

      尝试打印出(使用echo)整个字符串。

      echo "<div class=\"box middle offer alignleft\">\n<p class=\"detail alignleft\">" . $volum . "</p>\n<p class=\"detail alignleft\">" . $produsent . "</p>\n" . wpfp_link() . "</div>";
      

      【讨论】:

        【解决方案6】:

        这是导致任何问题的原因吗:

        $offer_html .= '</div'; 
        

        应该是

        $offer_html .= '</div>'; 
        

        【讨论】:

          【解决方案7】:

          我为回答两次而道歉,我注意到所选答案的代码存在错误。考虑以下代码:

          <?php
          function wpfp_link() {
              static $count = 0;
              echo '<img class="wpfp-img" title="Favorite" alt="Favorite" src="http://localhost:8888/wordpress/wp-content/plugins/wp-favorite-posts/img/remove.png">' . ($count++) ."\n";
          }
          // Turn on outbut buffering
          ob_start();
          
          
          function your_function()
          {
              // Fill outbut buffer
              wpfp_link();
          
              // Fetch and clean output buffer
              $wpfp_html = ob_get_clean();
              var_dump($wpfp_html);
              $offer_html = '<div class="box middle offer alignleft">';
              $offer_html .= '<p class="detail alignleft">' . $volum . '</p>';
              $offer_html .= '<p class="detail alignleft">' . $produsent . '</p>';
          
              // Use stuff from output buffer instead of wpfp_link() method.
              $offer_html .= $wpfp_html;
          
              $offer_html .= '</div>';
          
              return $offer_html;
          }
          
          // Don't forget to handle active output buffering!
          ob_end_flush();
          echo your_function();
          echo your_function();
          echo your_function();
          echo your_function();
          echo your_function();
          echo your_function();
          echo your_function();
          
          ?>
          

          变量 $wpfp_html 对于 your_function 的每次调用都有一个 false 值,因为在调用 ob_end_flush 后输出缓冲被关闭。每次都需要在 ob_get_clean() 之前调用 ob_start() 以使输出缓冲区处于活动状态。

          我会为你的回答发表评论,Informant,但我没有足够高的声誉。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-12-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-03
            • 2011-10-04
            • 1970-01-01
            • 2011-09-28
            相关资源
            最近更新 更多