Simple File Access Question


I just started doing some PHP and was having problem with this. I'm trying to open a web page "lod.htm" and read all the lines until it reads <body> I tried doing it similar to how I do it in Visual Basic, but I keep getting infinite loops.

$fp = fopen("lod.htm","r");
while($etc != "<body>")
$etc = fgets($fp);
print $etc;

LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


$fp = fopen("lod.htm","r");
$etc = "";
while (!feof($fp)) {
  $buffer = fgets($handle, 4096);
  if(stristr($buffer, "<body>")) {
  $etc .= $buffer;
print $etc;

however if there's anything before body on the same line, it won't be stored. the script could be modified to find the position of <body> within the string and then have the content of the string before this position stored in $etc. tell me how it goes

Timbo87Author Commented:
Maybe I should clarify what I need to do.

I want it to open an HTML file and put everything from <head> to </head> in one textarea and everything from <body> to </body> in another text area. It needs to be able to check the first 5 characters because it wont always be <body>, but sometimes <body bgcolor="white">.

Would it be possible to open the whole document with

$hcode = file ('lod.htm');

and then split the array between </head> and <body>?
i suppose you could do

$file = file_get_contents("lod.htm");
$exclude = explode("</head>", $file);
$head = $exclude[0];
$body = $exclude[1];
<form etc..
<textarea name="head"><? print $head; ?></textarea><br />
<textarea name="body"><? print $body; ?></textarea><br />

to remove the <html> and </html> you could use substr(); to take off the correct number of letters (and the \n line break) from the string, or you could explode the head and body variables again.

$top = explode("<html>", $head);
if(substr($top[0], 0, 5) == "<head) {
  $head = $top[0];
} else {
  $head = $top[1];
$bottom = explode("</html>", $body);
$body = $bottom[0];


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Timbo87Author Commented:
Thanks for the code loz, works great. I'm having trouble with the <html> </html> remover. I inserted it after $body = $exclude[1]; and it says there's a parse error.
Timbo87Author Commented:
Nevermind, found the problem and fixed it.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.