Solved

Simple File Access Question

Posted on 2003-11-16
5
241 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now