PHP - 如何加载HTML文件?

Joe*_*ter 8 html php

目前我有这样的文件

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo "<html>My HTML Code</html>";
}
?>
Run Code Online (Sandbox Code Playgroud)

但我想做这样的事情来保持我的PHP文件简洁.

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    //print the code from ..html/myFile.html
}
?>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Nau*_*hal 16

将您的HTML内容保存为单独的模板并简单地包含它

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("your_file.html");
}
?>
Run Code Online (Sandbox Code Playgroud)

要么

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    readfile("your_file.html");
}
?>
Run Code Online (Sandbox Code Playgroud)

readfile 更快,更少的内存密集 file_get_contents


小智 11

你可以看一下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');
Run Code Online (Sandbox Code Playgroud)