Page 1 of 1

read html file between <body> tags

Posted: Tue May 18, 2004 11:16 am
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

Posted: Tue May 18, 2004 11:31 am
by lostboy
read the entire page into a string ans split on <body>, then take that string and split ot on </body>...

Posted: Tue May 18, 2004 12:41 pm
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] ;)

Posted: Tue May 18, 2004 12:51 pm
by jds580s
lostboy & mudkicker

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

Justin

Posted: Tue May 18, 2004 2:45 pm
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

Posted: Tue May 18, 2004 2:47 pm
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";
?>

Posted: Tue May 18, 2004 3:33 pm
by lostboy
$htm=explode("<body ",$file);
$html=explode("</body>",$htm[1]);

Posted: Tue May 18, 2004 3:53 pm
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

Posted: Tue May 18, 2004 5:36 pm
by feyd

Code: Select all

$innerhtml = preg_split("/<\/?body[^>]*?>/i",$file);

Posted: Tue May 18, 2004 9:58 pm
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

Posted: Tue May 18, 2004 10:01 pm
by feyd
actually, the correct notation is in my post, but the forum highlighting tossed the ?> bits.

$innerhtml = preg_split("/<\/?body[^>]*?>/i",$file);

Posted: Tue May 18, 2004 10:04 pm
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:

Posted: Tue May 18, 2004 10:08 pm
by jds580s
:D
Great answers (and so quick) from both of you.
Worked like a charm.

Thanks to all for helping me out.

Justin