-2

I'm new to php. The thing I'm trying to accomplish here is to see what is the number of the last line in my html document (1,2,3...) and put that value into a variable and write

Hello world

to the html document but 2 or 3 lines above the last line. So right above the body close tag. I have been trying for days now and nothing seems to work.

sampletext.html

    <html>
<head>
     <title>WEB</title>
   </head>
 <body>
   <p>WEB test 335</p>
 </body>
    </html>

This one doesn't output:

<?php
$file = new SplFileObject('sampletext.html');
$file->seek(3);     // Seek to line no. 4
echo $file->current(); // Print contents of that line
?>

I also tried this one ,but it only writes at the end of the file:

<?php
 $file = fopen("sampletext.html","a");  //I also tried w,a+,r,r+ 
 $txt = "Hello world";
 fseek(fp, 0, SEEK_END);
 fwrite($file, $txt);
 fclose($file);
 ?>

There are a more but I can't seem to find them in my testing folder. I have read somewhere that it's possible to move the last few lines into temp memory and fwrite something and then move the lines from temp memory back into the file.

As I mentioned before I am very new to php and don't fully understand it yet!

Leon Kunštek
  • 555
  • 1
  • 7
  • 21
  • 1
    Please post the sample code you have. For help: https://stackoverflow.com/help/mcve – Bear Feb 14 '19 at 18:21
  • 1
    Please take a look at [how to ask](https://stackoverflow.com/help/how-to-ask) and consider editing your question. Stack Overflow isn't a place to get people to write code for you, it's a place to get assistance with your code. Show what you've tried, how it failed, and what direction you think you need to go in. The more effort you put into your question, the more effort people will put into their answers. – 3ocene Feb 14 '19 at 18:22
  • 1
    And welcome to Stack Overflow by the way – 3ocene Feb 14 '19 at 18:23
  • It would probably be better to write into a specific DOM element, rather than counting lines. You can use `DOMDocument` to parse the document and access parts of it. – Barmar Feb 14 '19 at 18:23

3 Answers3

0

Provided that this is something you would actually want to do, I would create an array of all lines in the read file, determine it's length, change the content of the third last array item and output everything in a loop.

That said, the way you seem to want to use PHP seems very non-standard. If you would read a bit about it, I bet you would come up with much more elegant solutions than this. A good starting point would be w3schools

UncleB
  • 46
  • 2
0

The only thing that I found that works is to delete the last few lines of the code, write what you wanted and then write the code you deleted or use a php function that reads through the file and looks for a specific code (variable or something else but it shouldn't repeat in that context) and change it with another bit of code.

Leon Kunštek
  • 555
  • 1
  • 7
  • 21
-1

Try this to get last line: PHP - Returning the last line in a file?

And for write to last line try: Delete last line and check data in text file

Martys
  • 20
  • 6
  • Even if the answer is in those links, link only answers provide not future-proof solution. Those links could die within a week and your accepted answer is actually two 404s. It's best to take from those resources the info that provides the solution to create an everlasting (we hope) solution on stackoverflow – treyBake Feb 14 '19 at 18:50