Importing an html file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Importing an html file

Post by jayr517 »

Hey all. Does anyone know of a way to import an html file into html that's already written?

For example, I have this code:

Code: Select all

<HTML>
<HEAD>
<TITLE>Under Construction</TITLE>
<LINK rel="stylesheet" type="text/css" href="./css/default.css"/>
<?php
$headerStyle     = "";
$trailerStyle    = "";
$navbarStyle     = "";
$banner          = "";
$bannerLink      = "";
require_once("./php/HeaderClasses.php");
require_once("./php/TrailerClasses.php");
require_once("./php/NavigationClasses.php");
$header  = new Header($headerStyle, $banner, $bannerLink);
$trailer = new Trailer($trailerStyle);
$navbar  = new NavBar($navbarStyle);
?>
<SCRIPT src="./js/common.js"></SCRIPT>
</HEAD>

<BODY>
<TABLE class="mainpage">
    <?php
        // header...
        $header->display();
    ?>
    <TR>
        <?php
            // Nav Bar...
            $navbar->display();
        ?>
        <TD class="mainpage">
            <?php
                // Body...
                THIS IS WHERE I WANT TO IMPORT THE HTML FILE.
            ?>
        </TD>
    </TR>
    <?php
        // trailer...
        $trailer->display();
    ?>
</TABLE>
</BODY>
</HTML>
Note...the html file I want to import is a full file (which includes the open and close html, title, head, and body tags).

Anyone, anyone...beuller, beuller???
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I have no idea why you would want to do that. And don't forget that importing someone else's webpage into your webpage could be in breach of copyright (if you are doing that).

Why don't you just use an <iframe> ?
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

Well thank you for the law review, but I'm aware of that...;-)

I don't want scroll bars in the middle of my page, so IFRAMES don't work.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

jayr517 wrote:Well thank you for the law review, but I'm aware of that...;-)

I don't want scroll bars in the middle of my page, so IFRAMES don't work.
Ok fine. So where shall we start from, importing the file into PHP or getting everything between the <body></body> tags once it's loaded?
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

ty...I really just want to get everything between the body tags. I actually tried just including the html file (include("./file.htm")) and it seemed to work ok, although my css attributes didn't apply. I'd rather not do that though, because it certainly is "standard" and I don't want to open myself up for weird things starting to happen with the site.

Is parsing the file the only way?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I'll give you the basics.....

Code: Select all

<?php

$file = "somepage.htm";

// Load the file into a variable.
$o = open($file, "rb");
$c = fread($o, filesize($file));
fclose($o);

// Get stuff between the body tags.
$sA = explode("<body>", $c);
$sB = explode("</body>", $sA[1]);
$contents = $sB[0];

// Echo the contents.
echo $contents;

?>
You might also want to check out.......

http://se.php.net/manual/en/function.fopen.php
http://se.php.net/manual/en/function.fread.php
http://se.php.net/manual/en/function.fclose.php
http://se.php.net/manual/en/function.explode.php

........for reference.
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

That's great, thanks a lot for the help.
Post Reply