PHP - 如何加载 HTML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15270327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - how to load HTML files?
提问by Joe Slater
At the moment I have a file like this
目前我有一个这样的文件
<?php
if(some condition)
{
//Dont allow access
}
else
{
echo "<html>My HTML Code</html>";
}
?>
But I wanted to do something like this to keep my php file short and clean.
但我想做这样的事情来保持我的 php 文件简短干净。
<?php
if(some condition)
{
//Dont allow access
}
else
{
//print the code from ..html/myFile.html
}
?>
How can I achieve this?
我怎样才能做到这一点?
回答by Nauphal
save your html content as seperate template and simply include it
将您的 html 内容另存为单独的模板并简单地包含它
<?php
if(some condition)
{
//Dont allow access
}
else
{
include ("your_file.html");
}
?>
OR
或者
<?php
if(some condition)
{
//Dont allow access
}
else
{
readfile("your_file.html");
}
?>
readfileis faster and less memory intensive than file_get_contents
readfile比 file_get_contents
回答by Prijm.com
you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:
您可能会查看PHP Simple HTML DOM Parser,这似乎是满足您需求的好主意!例子:
// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');
// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');
// Create a DOM object from a HTML file
$html = file_get_html('test.htm');
回答by Tsimtsum
Use this code
使用此代码
if(some condition)
{
//Dont allow access
}
else
{
echo file_get_contents("your_file.html");
}
OR
或者
if(some condition)
{
//Dont allow access
}
else
{
require_once("your_file.html");
}
回答by asprin
Extending nauphal's answer for a more robust solution..
扩展 nauphal 的答案以获得更强大的解决方案..
<?php
if(some condition)
{
//Dont allow access
}
else
{
if(file_exists("your_file.html"))
{
include "your_file.html";
}
else
{
echo 'Opps! File not found. Please check the path again';
}
}
?>
回答by DonCallisto
<?php
if(some condition)
{
//Dont allow access
}
else
{
echo file_get_contents("your_file.html");
}
?>
This should do the trick
这应该可以解决问题
Or, as nauphal's answer say, simply use include()
或者,正如nauphal的回答所说,只需使用include()
Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)
不要忘记,如果文件不存在,你可能会遇到一些麻烦(所以,也许,在包含或获取内容之前检查)
回答by Sumit Bijvani
Use functions like
使用函数,如
include()
include_once()
require()
require_once()
file_get_contents()
回答by Scott Sellers
I think you want to include your HTML file or have I misunderstood the question.
我认为您想包含您的 HTML 文件,或者我误解了这个问题。
<?php
if(some condition)
{
//Dont allow access
}
else
{
include ("..html/myFile.html");
}
?>
回答by Andrej Bestuzhev
Way 1:
方式一:
ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();
echo $return;
Way 2: Use templaters, like CTPP, Smarty, etc... Templaters are useful to transfer some logic from php to template, for example, in CTPP:
方式二:使用模板器,如CTPP、Smarty等...模板器可用于将一些逻辑从 php 转移到模板,例如,在 CTPP 中:
$Templater -> params('ok' => true);
$Template -> output('template.html');
in template html:
在模板html中:
<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>
The same ideas are in other templaters. Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.
其他模板器中也有相同的想法。模板器更好,因为它可以帮助您标准化模板并将所有原始逻辑发送给它们。

