如何对php文件的一部分使用file_put_contents()

如何对php文件的一部分使用file_put_contents(),php,html,Php,Html,我正在使用PHP和html来开发一个简单的机制,创建报告并通过电子邮件发送。 我使用函数file\u put\u contents()和函数ob\u get\u contents()作为参数来创建一个html文件,用于通过邮件发送。 我意识到,如果我使用ob\u get\u contents()而不使用ob\u start(),那么它只需要将所有文件放入html文件中。这对我不好,因为我只希望文件的一部分在生成的html中。更清楚地说,我的代码如下所示: <html and php cod

我正在使用PHP和html来开发一个简单的机制,创建报告并通过电子邮件发送。 我使用函数
file\u put\u contents()
和函数
ob\u get\u contents()
作为参数来创建一个html文件,用于通过邮件发送。 我意识到,如果我使用
ob\u get\u contents()
而不使用
ob\u start()
,那么它只需要将所有文件放入html文件中。这对我不好,因为我只希望文件的一部分在生成的html中。更清楚地说,我的代码如下所示:

<html and php code I want to include in my html file>
.
.
<html and php code I don't want to include in my html file>
.
.
<html and php code I want to include in my html file>
.
.
<html and php code I don't want to include in my html file>
.
.
.

file_put_contents('report.html', ob_get_contents());
$message = file_get_contents('report.html');
mail($to, $subject, $message, $Headers);

.
.
.
.
.
文件内容('report.html',ob_get_contents());
$message=file_get_contents('report.html');
邮件($to、$subject、$message、$Headers);
那么,如何只选择要包含在report.html中的部分呢


多谢各位

您这样做是不必要的困难,您不需要外部文件来生成报告。看看这个:

<?php
$report = '';

// ...
// Code not included in your report
// ...

ob_start();
// ...
// HTML and PHP code you want in your report
// ...
$report .= ob_get_clean();

// ...
// Code not included in your report
// ...

ob_start();
// ...
// HTML and PHP code you want in your report
// ...
$report .= ob_get_clean();

// Mail it
mail($to, $subject, $report, $headers);
?>

编辑:关于OP的评论

您需要的是
ob\u get\u flush()
,而不是
ob\u get\u clean()
。两者都以字符串形式返回缓冲区内容,但第一个将其转储到脚本输出,而第二个将清空缓冲区。

可能有帮助,也可能没有帮助。


在使用vanila PHP时,我总是以加载页面的方式处理这个问题,并附带一个代码段!以下是我一直保留并使用的一个。它有两个可能的主要功能。一个是加载一个视图(html页面),另一个是获取一个html页面作为字符串,用于电子邮件正文中包含的内容

例如:

//  will load a page into the clients browser
//    note the page location would indicate it will get the file "index.html" from "$_SERVER["DOCUMENT_ROOT"] . '/views/'"
loadView('/views/index.html');

//  or, as would be more helpful to you
$msgHTML = loadView('/views/index.html', NULL, TRUE);
TRUE
参数只是告诉函数只返回字符串,而不向客户端回显任何内容

您看到的
NULL
参数用于传递数据数组。例如,假设您有一个html页面,其中包含一个要为数据库调用填充的表。您只需拨打电话,将报税表放入数组中,然后添加到页面

$arr = array( 'key' => 'value' );
$msgHTML = loadView('/views/index.html', $arr, TRUE);

//  then in the index.html
<div><?= $key; ?></div>
因此,您可以执行以下操作:

$htmlFirst = loadView('report.html', NULL, TRUE);
$msgFirst = 'Some message string here';
$htmlSecond = loadView('report2.html', NULL, TRUE);
$msgSecond = 'Some message string here';

$body = $htmlFirst . $msgFirst . $htmlSecond . $msgSecond;
mail($to, $subject, $body, $Headers);

为什么不通过执行$html='来“编写”html呢?或者,您可以创建多个PHP文件,其中包含大型html的一小部分。这样,您就可以准确地加载所需的文件。这或多或少与Kei的方法相同,只是更干净,没有内联HTMLH。这是一个很好的解决方案,对我很有效。问题是,我也在GUI中呈现php文件(我呈现整个php文件,并且仅通过电子邮件发送一部分)。当我使用这种格式时,它会隐藏文件的所有内容,我只能通过电子邮件看到它。有没有一种方法既可以写入缓冲区,也可以写入屏幕?(我希望我的问题很清楚)
$htmlFirst = loadView('report.html', NULL, TRUE);
$msgFirst = 'Some message string here';
$htmlSecond = loadView('report2.html', NULL, TRUE);
$msgSecond = 'Some message string here';

$body = $htmlFirst . $msgFirst . $htmlSecond . $msgSecond;
mail($to, $subject, $body, $Headers);