It's quite usual to use the include function of php to include html files into another html or php file. In order for the resulting output to be a correct html output, the html header should appear only once (and outside the body). Thus, the included html files should not contain the header, but only the body contents. If, however, you write all these html pieces using a WYSIWYG editor, these html will end up having their own header.
Thus, when including these html pieces from a php file, you should strip header (and closing html tags), and get only the contents included in the body tag. By using regular expressions it's pretty easy to do that.
This is my simple implementation:
<?php
/**
* Returns the body content of the specified HTML file.
*
* @param string $filename
* @return string
*/
function getbody($filename) {
$file = file_get_contents($filename);
$bodypattern = ".*<body>";
$bodyendpattern = "</body>.*";
$noheader = eregi_replace($bodypattern, "", $file);
$noheader = eregi_replace($bodyendpattern, "", $noheader);
return $noheader;
}
?>
10 comments:
Thanx,
I am not very good in PHP, but would love to grab a body from another server. For example, if a would like to grab the body of: "http://domain.com/test.html" , how will the PHP code look like????
Could you help me and give this in an example? Thanx,
Stijn
well... you just need to call the above function in a php script...
Thanx Bretto,
But I am a real newbe... :(
Isn't it possible to include a little script in the HTML of my own page. So just a few lines in the html of the page example.php?
Where do I have to place the "http://domain.com/test.html"?
Thanx again!
All you need to do is to create a php file (say example.php) with the following content:
... copy the function getbody ...
echo getbody("http://domain.com/test.html");
remember to put anything between the php tags (i.e., < ? php and the closing ? > I cannot put it into this post since the system does not allow it)
is this what you needed?
echo getbody("page.html");
this is not equal to
include("page.html");
If you have php code inside page.html, this code is not processed!
is there a method for this case?
you're right about that, but I don't know a solution for this case...
very nice code....
as far as Anonymous question....you can not have php code inside a html document .... you must save it as .php and then run the getbody()
;)
How would you handle this now that eregi_replace has been deprecated.
I've struggled trying to replicate this using preg_replace because it is looking for delimiters.
Any clues?
thank you very much for the script, it is working for me !!
Alex
I remember exactly how I started my first work with HTML. Unfortunately, I stopped at this step and now fortunately there are special companies that provide IT solutions. At the moment I have already used the services of grapeup.com/services/application-development so I know what good work of applications in the cloud means.
Post a Comment