Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple File Access Question

Posted on 2003-11-16
5
Medium Priority
?
280 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 100 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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…
Suggested Courses

926 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