fireineyes Posted September 18, 2008 Share Posted September 18, 2008 I want to include html files but I want to remove ranges of text upon include. Like everthing between <html> and <body> including those tags... here is what I am looking at.. 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) <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.. . 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.) 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...) 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. Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 19, 2008 Share Posted September 19, 2008 The only thing I can think of doing is using a regular expression like this (written by me - quite sloppy, though): <?php $str = preg_replace('/\<(/?)(html|head|title|body)(([([a-zA-Z0-9])])=(([a-zA-Z0-9])|"([a-zA-Z0-9])"|\'([a-zA-Z0-9])\'))\>/', '', $str); //To add more tags, just put the name of the tag in the (html|head|title|body) set. Be sure to separate tag-names with a pipe (|) ?> Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 19, 2008 Share Posted September 19, 2008 Terribly sorry for the double post (the time limit for the post modification ran out): The only thing I can think of doing is using a regular expression like this (written by me - quite sloppy, though): <?php $str = preg_replace('/\<(/?)(html|head|title|body)(([([a-zA-Z0-9])])=(([a-zA-Z0-9])|"([a-zA-Z0-9])"|\'([a-zA-Z0-9])\'))\>/', '', $str); //To add more tags, just put the name of the tag in the (html|head|title|body) set. Be sure to separate tag-names with a pipe (|) ?> You'll have to use some file-handling functions too; refer to the PHP Manual for such functions: http://ca.php.net/manual/en/ref.filesystem.php <?php $file = fopen('Example.html', 'r+'); $text = fread($file); $str = preg_replace('/\<(/?)(html|head|title|body)(([([a-zA-Z0-9])])=(([a-zA-Z0-9])|"([a-zA-Z0-9])"|\'([a-zA-Z0-9])\'))\>/', '', $text); $write = fwrite($file, $str); fclose($file); include 'Example.html'; ?> I've never really worked with file-handling functions before, or even done something like this. I'm not sure if it'll work at all. Just keep in mind, if you do that you'll be permanently stripping those tags out. I can't think of a way to do it temporarily. Well you could try using a cache to store an instance of a file, but I'm not sure how you'd go about that. Why don't you just remove the code manually and not even bother to use it in the future? Quote Link to comment Share on other sites More sharing options...
fireineyes Posted September 19, 2008 Author Share Posted September 19, 2008 Because the site uses Frontpage extensions on a lot of files and I was needing these to include in some php file. I will look at this and see what I can do. I could go in and redo the site in all php but I dont have the time at the moment. Would it be best to copy the file regularly and have a cron set up, and at same time have the regular expression strip out automatically what I need it to? Quote Link to comment Share on other sites More sharing options...
Guest Xanza Posted September 19, 2008 Share Posted September 19, 2008 Firstly you need to ween yourself off HTML editors.... They're useless - what would you do without one? It's like a calculator; if you use them, and only them without learning the process on paper, when it comes time for SAT's what the hell are you going to do when you can't use a calculator? The best and most preferred way to learn any programming language is from scratch... Once you've done that, you'll not only understand the constructive programming hierarchy better, but you'll be able to better grasp what you're trying to accomplish. Secondly make sure of two things. Considering you want to include a html file, make sure that any files that include PHP syntax are called "document.php", and not "document.html". If they are named for HTML they will only parse HTML, and treat PHP syntax like normal text. Other than that just make sure your server is able to parse PHP. Hope this helped! Quote Link to comment Share on other sites More sharing options...
fireineyes Posted September 26, 2008 Author Share Posted September 26, 2008 the extension makes no difference. I make all the .html and .htm files parse php via editting my .htaccess file. As far as learning html from scratch, that is how I learned is by notepad back in 1997. I can code in CSS and I can do a lot in php (probably more than most newbies around.. learned on notepad as well.. can do everything form if else statement to arrays ect :-) but I was not sure how to include without ranges of text. Suffice to say, I could code circles around most people when it comes to html/css and probably plenty of people in php. I started using FrontPage online editting for the speed. Many of my older sites have it, so why reinvent the wheel when the wheel turns. I can build other sites in the meantime. I will most likely just create seperate "template" files to include and just try to remember to edit both files when updating... simplest thing I suppose. Just was wondering.. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 26, 2008 Share Posted September 26, 2008 The easiest way would be to do something like this: $start = stripos( $html, '<body>' ) + 6; $end = stripos( $html, '</body>' ); $content = substr( $html, $start, $end-$start ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.