使用file_get_contents将本地PHP文件的HTML输出存储到字符串中 [英] Storing the HTML output from a local PHP file into a string using file_get_contents

查看:232
本文介绍了使用file_get_contents将本地PHP文件的HTML输出存储到字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个header.php文件,它包含一些返回HTML的php代码. 我知道我可以使用require,include来回显结果,但是我要做的是将其处理后的输出字符串存储到变量中.

There is a header.php file and it contains some php codes that return HTML. I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.

在一个页面中,我使用了:

In a page, I used:

$headerHTML=file_get_contents('header.php');

然后我得到了PHP代码输出,而不是经过处理的HTML输出. 我知道添加http://会有所帮助. 但是我更喜欢继续使用相对路径,如何告诉函数正确处理php文件?

Then I got the PHP code output rather than the processed HTML output. I know adding http:// would help. But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?

注意:如果可能,我想继续使用此语句file_get_contents而不是使用ob_start().

Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.

推荐答案

我宁愿使用包装在ob_start()ob_get_clean()内的require().我相信这种方法没有错.

I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.

这篇关于使用file_get_contents将本地PHP文件的HTML输出存储到字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆