Solved

Simple File Access Question

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 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