输出缓冲:从HTML代码制作字符串的简便方法

时间:2018-09-01 23:30:46

标签: php html string output-buffering

我目前正在使用Chatfuel打开我网站的index.php文件,该文件会将html代码发送到用户的浏览器中。他可以在那里注册并设置帐户。 一个示例URL可能看起来像这样:

https://my.domain.com?key_value='123456789'

根据该用户是新用户还是现有用户,我想给他提供另一种形式。为了进行检查,我对MySQL db进行了简单查询,看看传递的key_value是否已在db中,并且对布尔值安全为true或false。说明以下事实:如果他不是现有用户,则应显示没有值的“空”表单。如果他已经注册,他应该会看到他上次填写的信息。

我的想法: 在index.php的顶部,我检查他是否是现有客户(注意:这已经在起作用)。然后,我想使用outputbuffering根据布尔值更改html代码,然后再将其发送到客户端。

我的问题: 我用纯HTML开发了网站的蓝图(请参见下面的代码)。并且OB仅在字符串内捕获它作为输出。由于我在文档中同时使用"',因此字符串每隔几行就会被打断。有一个简单的解决方法吗?因为OB函数无法访问<html>...</html>标记内的任何内容。 还是我需要在检查之后(在我的index.php中)使用重定向并为编辑客户数据和添加新客户数据创建单独的表单+脚本?

<?php

//Connection stuff


// Prepare statment: !TODO: string needs to be escaped properly first
$query_string = "SELECT * FROM tbl_customer WHERE unique_url = '$uniqueurl'";
$query_rslt = mysqli_query($conn, $query_string);

if($query_rslt == FALSE)
{
    // Failure
    echo "<br> Oops! Something went wrong with the querying of the db. " . $conn->connect_error;
    //Handle error
}
else
{
    if ($query_rslt->num_rows > 0)    
    {
        // Set boolean
        $existing_customer = TRUE;

        // Create an array called row to store all tuples that match the query string
        while($row = mysqli_fetch_assoc($query_rslt)) {
            //...
        }
    }
}

// Custom post processing function
function ob_postprocess($buffer)
{
    // do a fun quick change to our HTML before it is sent to the browser
    $buffer = str_replace('Testing', 'Working', $buffer);

    // Send $buffer to the browser
    return $buffer;
}

// start output buffering at the top of our script with this simple command
// we've added "ob_postprocess" (our custom post processing function) as a parameter of ob_start
if (!ob_start('ob_postprocess'))
{
    // Failure
    echo "<br> Oops! Something went wrong with output buffering. Check that no HTML-Code is sent to client before calling this start function.";
    // Handle error
}
else
{
    // Success
    // This is where the string should get accessed before sending to the client browser
    echo "Testing OB.";
}

?>

<!--DOCTYPE html-->
<html lang="en">
    <head>
        <meta charset="utf-8">
        //...
</body>
</html>

<?php

// end output buffering and send our HTML to the browser as a whole
ob_end_flush();

?>

输出: "Working OB."

编辑:我添加了源代码示例。该代码将无法编译。

2 个答案:

答案 0 :(得分:0)

因为我无法发表评论,所以我在这里提出一些问题。

我真的不明白这一点,但是请尝试一下,您是说escaping字符串吗?您可以使用反斜杠\来转义字符串。

喜欢"select from ".$dbname." where id = \"".$id."\""


在将变量添加到addslashes($var)之前,您可以轻松使用sql。像这样

$id = addslashes($_POST['id']);
$sql = "select form db where id = '$id'";

如果您要检查用户的存在以选择要在页面中显示的表单,那么为什么不这样做?

if(userCheck()) {
  ?>
    // here write the html code if user passed
  <?php
} else {
  ?>
    // here write the html code if user not passed
  <?php
}

您可以将userCheck()设置为全局函数,也可以放置在任何位置,只要您想在显示form之前检查用户就可以使用它。

答案 1 :(得分:0)

tl; dr:我要寻找的是file_get_contents()和对象缓冲的组合。

file_get_contents()返回您选择的纯html文件的字符串。我可以在这里发表大量解释,或者直接将您链接到phppot.com。本文为您提供了带有源(Download here)的直接可执行演示。如果您想使用自己的html文件来尝试,只需更改文件路径即可。

因此,一旦将整个html转换为字符串,如果已有用户来更改其数据,则我将使用OB的后处理功能来更改字符串(基本上是我的html)。然后,所有的html代码(此时仍为字符串)都使用ob_end_flush()发送给客户端。我将尽快编写实际的代码:)