My HTML Co...">

PHP - how to load HTML files?

At the moment I have such a file

<?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
if(some condition)
{
    //Dont allow access
}
else
{
    //print the code from ..html/myFile.html
}
?>

How can i achieve this?

+5
source share
8 answers

you can take a look at PHP Simple HTML DOM Parser seems like a good idea for your needs! Example:

// 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');
+10
source

save your html content as a separate template and just include it

<?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");
}
?>

readfile faster and less memory than file_get_contents

+15
source

if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}

if(some condition)
{
    //Dont allow access
}
else
{
    require_once("your_file.html");
}

+3
<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}
?>

, nauphal , include()

, , (, , )

+2

,

include()
include_once()
require()
require_once()
file_get_contents()
+2

, HTML .

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("..html/myFile.html");
}
?>
+1

.

<?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';
    }
}
?>
+1

1:

ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();

echo $return;

2: templaters, CTPP, Smarty .. Templaters php , , CTPP:

$Templater -> params('ok' => true);
$Template -> output('template.html');

html:

<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>

. Templaters , .

-1

All Articles