Solved

Simple File Access Question

Posted on 2003-11-16
5
251 Views
Last Modified: 2006-11-17
Hello,

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;
}
?>

Thanks.
0
Comment
Question by:Timbo87
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:lozloz
ID: 9761040
try:

<?
$fp = fopen("lod.htm","r");
$etc = "";
while (!feof($fp)) {
  $buffer = fgets($handle, 4096);
  if(stristr($buffer, "<body>")) {
    break;
  }
  $etc .= $buffer;
}
fclose($fp);
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

loz
0
 
LVL 15

Author Comment

by:Timbo87
ID: 9761140
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>?
0
 
LVL 13

Accepted Solution

by:
lozloz earned 25 total points
ID: 9761169
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 />
</form>

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];

loz
0
 
LVL 15

Author Comment

by:Timbo87
ID: 9761277
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.
0
 
LVL 15

Author Comment

by:Timbo87
ID: 9761335
Nevermind, found the problem and fixed it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question