read html file between <body> tags

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
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

read html file between <body> tags

Post by jds580s »

I'm trying to find a way to rid a site of an inline frame using php.

My thought was to set up a table where the old i-frame was and have php insert the code between the <body> & </body> tags from the html file.

I'm a little stumped on how to go about reading in the file between those tags so there are not 2 headers in the resulting file. I've googled and searched the forums as much as I could, but I'm not certain I know what I'm looking for. Any help is greatly appreciated.

Justin
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

read the entire page into a string ans split on <body>, then take that string and split ot on </body>...
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

lostboy wrote:read the entire page into a string ans split on <body>, then take that string and split ot on </body>...
You do this with [php_man]explode[/php_man] ;)
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

lostboy & mudkicker

Thanks very much for the push in the right direction. It's much appreciated

Justin
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

one more related question. I have it working as long as my <body> tag is empty. What might I use if the tag has some peramiters in it such as:
<body class="whatever">

I think I'm looking for something like a wildcard or regular expressions that can look for the beginnig characters "<body" then any number of random alpha numeric characters up until the ">" But it's eluding me.

Thanks again for any help.

Justin
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

Might help if I posted the code I'm using now

Code: Select all

<?php
$file=file_get_contents("tutorials/web/web_server.html", "r");
$htm=explode("<body>",$file);
$html=explode("</body>",$htm[1]);
$body=$html[0];
print "$body";
?>
Last edited by jds580s on Tue May 18, 2004 4:55 pm, edited 1 time in total.
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

$htm=explode("<body ",$file);
$html=explode("</body>",$htm[1]);
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

Ahh, I see I need to re-word my issue because you answered what I asked but I didn't ask what I wanted. :)

My body tag looks like this
<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
but it won't always have the same stuff in it.

this:

Code: Select all

$htm=explode("<body ",$file); 
$html=explode("</body>",$htm[1]);
obviously returns
leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
and then the rest of the document as expected.

I'm curious if I can include all the characters up to and including the ">" that closes the body tag as part of the first "explode"

I hope that clarifies things a bit.

Justin
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$innerhtml = preg_split("/<\/?body[^>]*?>/i",$file);
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

I'm really out of my realm here so I appologize for all the basic questions. I'm trying to get a grasp on this, and it's starting to come together.

feyd, I'm using your code now and all is good but I can't seem to get the trailing ">" in the <body> tag to split. It's being included in the rest of the document.

I've been reading about "pattern syntax" in the docs and tried changing a few things around but I don't have the right combination.

Code: Select all

$innerhtml = preg_split("/<\/?body[^>]*/i",$file);
am I correct in thinking that the "^" character
defined as: "negate the class, but only if the first character"
is the problem?

Again, thanks very much for any help on this.

Justin
Last edited by jds580s on Wed May 19, 2004 10:04 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

actually, the correct notation is in my post, but the forum highlighting tossed the ?> bits.

$innerhtml = preg_split("/<\/?body[^>]*?>/i",$file);
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

well if I can beat feyd, all you would need is to add the '?' syntax

Code: Select all

body&#1111;^>]*?>
grrrrrrrr, why did I even bother!?!?!?!

:wink:
jds580s
Forum Newbie
Posts: 7
Joined: Tue May 18, 2004 11:16 am

Post by jds580s »

:D
Great answers (and so quick) from both of you.
Worked like a charm.

Thanks to all for helping me out.

Justin
Post Reply