Want to Include but Omit parts of code from files.

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
fireineyes
Forum Newbie
Posts: 2
Joined: Sat Nov 01, 2003 12:10 pm

Want to Include but Omit parts of code from files.

Post by fireineyes »

Want to Include but want to omit parts of file. A range like between <html> and <body> removing both tags as well
This is what i am talking about. I have some pages that I currently include using Frontpage extensions. Those pages require the <html><head> and <body> tags. What I want to do is use the same files included using php but stripping out the code that goes from <html> to the end of the <body> code and also strips out the </body></html> code at the end. I am sure this is easy to do, but I am a newbie and it would be a great help if someone could suggest the best way to include files...

reason I ask this is when I include a file like example.html using php includes, is because it gives something like this...

Example.html (I am including)

Code: Select all

 
<html>
<head>
<title>title of page</title>
</head>
<body bgcolor="white">
 
This is content I want to include <img src="pic"> <p>
So on and so on... 
 
</body>
</html>
 



at moment when I do this code.. .

Code: Select all

 
text text text <p>
 
<? include ('Example.html'); ?> <p>
 
text2 text2 etc. etc... 
 
it returns this... (notice teh full html code is included from the include page.)

Code: Select all

 
text text text <p>
 
<html>
<head>
<title>title of page</title>
</head>
<body bgcolor="white">
 
This is content I want to include <img src="pic"> <p>
So on and so on... 
 
</body>
</html><p>
 
text2 text2 etc. etc... 
 
when I want it to look like...(notice the code stripped out from the body tag up and the closing body tag down...)

Code: Select all

 
text text text <p>
 
 
This is content I want to include <img src="pic"> <p>
So on and so on... 
 
<p>
 
text2 text2 etc. etc... 
 
Thanks and I know this is most likely very easy stuff. Thanks for the help.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Want to Include but Omit parts of code from files.

Post by Stryks »

I don't really understand the why of this .. but here is a solution.

There are a few other ways to do this, and although it seems like a bit of a waste to use regex for this, it's probably the simplest way to get around variations in your body tags.

This also assumes there will not be any PHP code in the included files. Would need to alter a little if inline scripting was needed.

Code: Select all

function include_html_body($filename) {
   if(!file_exists($filename)) {
      $data = file_get_contents($filename);
      if($data !=== false) {
         if(preg_match('%<body[^>]*>(.*?)</body>%si', $data, $result)) $data = trim($result[1]);
      }
      echo $data;
   }
}
So ...

Code: Select all

// so instead of 
<? include ('Example.html'); ?>
 
// you would just go
<? include_html_body('Example.html'); ?>
Hope that helps.
Post Reply