Jump to content

write between two points in a html file.


tomfmason

Recommended Posts

I am wanting to write, between two points, to a html file.

say I want it to start at <!--start--> and end at <!--end-->

This is what I have now.

[code=php:0]$file = "$nick.html";
$fp = fopen($file, 'r+'); 
$newline = "<b>$nick:</b> $message <br>";

while (!feof($fp)){
    $line = trim(fgets($fp, 322));
    if ($line == '<!--Start of chat-->'){
        $fpos = ftell($fp); 
        $rest = fread($fp, filesize($file));

        fseek($fp, $fpos, SEEK_SET); 
        fwrite($fp, "\n\n");
        fwrite($fp, $rest);
        fwrite($fp, $newline) 
}
}
fclose($fp);[/code]

I am wanting to write at the end of the file [code=php:0]<a name="end"></a>[/code]. This way I can jump to the last line in the file.

Any suggestions would be great.
Link to comment
https://forums.phpfreaks.com/topic/15098-write-between-two-points-in-a-html-file/
Share on other sites

then line number would change each time a message is posted.

here is the script that writes the $nick.html

[code=php:0]$filename = "$nick.html";
$handle = fopen($filename, "x+b");
$body = "<p>
Welcome <b>$nick</b>, an operator will be with you shortly.
</p>
<!--Start of chat-->
<!--End of chat-->
</p>";
fwrite($handle, $body);
fclose($handle);
include('chat.php')[/code]

I have tried writeing the new message like this

[code=php:0]$file = "$nick.html";
$fp = fopen($file, 'r+'); 
$newline = "<b>$nick:</b> $message <br>";

while (!feof($fp)){
    $line = trim(fgets($fp, 322));
    if ($line == '<!--Start of chat-->'){
        $fpos = ftell($fp); 
        $rest = fread($fp, filesize($file));

        fseek($fp, $fpos, SEEK_SET); 
        fwrite($fp, "\n\n");
        fwrite($fp, $newline)
        fwrite($fp, $rest);
 
}
}[/code]

This posts the new line like this line4 line3 line2 line1. I want it to start with the oldest line then move on to the new lines.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.