简体   繁体   English

如何将行插入到特定行号的文件中?

[英]How do you insert a line into a file at a specific line number?

In PHP there is fopen('r+') for Read/Write. 在PHP中,有fopen('r +')用于读/写。 (start at beginning of the file) There is also fopen('a+') for Read/Write. (从文件开头开始)还有用于读取/写入的fopen('a +')。 (append to the end of the file) (附加到文件末尾)

I made a simple $_POST['data'] form that writes whatever is written in the form to a file. 我制作了一个简单的$ _POST ['data']表单,将表单中写入的内容写入文件。 My problem is that if I write a few things, for example. 我的问题是,例如,如果我写一些东西。

'Red ' 
'Green ' 
'Blue '

If I append this data, it will show up in the file in the order I write in. 如果我追加此数据,它将按照我写入的顺序显示在文件中。

But, I need to write to the beginning of the file, so that it's in reverse order. 但是,我需要写入文件的开头,以便其顺序相反。

'Blue '
'Green '
'Red '

$file_data = 'What to write in the file'; $ file_data ='要写入文件的内容';

$file_data .= file_get_contents('testz.php'); $ file_data。= file_get_contents('testz.php'); file_put_contents('testz.php', $file_data); file_put_contents('testz.php',$ file_data);

  1. This will write whatever is in the $file_data variable to the file and then write the data that was originally in the file afterwards. 这会将$ file_data变量中的内容写入文件,然后再写入文件中原来的数据。

Thus causing it to show up before everything else that was originally in the file. 从而使它显示在文件中的所有其他内容之前。 This works fine. 这很好。


The problem starts when I want to include a line of code at the beginning of the file. 当我想在文件开头包含一行代码时,问题就开始了。

Specifically I want '!DOCTYPE html html head' to always be at the top of the document. 具体来说,我希望“!DOCTYPE html html head”始终位于文档的顶部。 I could of course put that in the file data like: file_put_contents('testz.php', $headerinformation.$file_data); 我当然可以将其放在文件数据中,例如:file_put_contents('testz.php',$ headerinformation。$ file_data);

But the problem with that is gonna be everytime I submit the POST, It's gonna be repeating 但是问题在于每次我提交POST时,都会重复

<!DOCTYPE html><html><head>Blue
<!DOCTYPE html><html><head>Green
<!DOCTYPE html><html><head>Red

What I want is 我想要的是

<!DOCTYPE html><html><head>
Blue
Green
Red

So, then I can keep writing more information to the file via the POST form WITHOUT REPLACING THE FIRST LINE. 因此,然后我可以继续通过POST表单将更多信息写入文件,而无需替换第一行。

<!DOCTYPE html><html><head>

I know this is gonna be kind of a bad question, what what exactly can I do to achieve this? 我知道这将是一个不好的问题,我该怎么做才能做到这一点?

Why don't you check if file is already with some data in it, for eg.: 为什么不检查文件中是否已经包含一些数据,例如:

$data_to_write = file_get_contents('testz.php'); 
$header_data = '<!DOCTYPE html><html><head>';
$file_data .= (empty($data_to_write))?$header_data.$data_to_write:$data_to_write;
file_put_contents('testz.php', $file_data);

You can only add to the end of a file. 您只能添加到文件末尾。 You can write in the middle of it but only writing over existing data. 您可以在其中写,但只能覆盖现有数据。 So in order to add lines of text always as the second line you should load the data, add the line in the right place, then write the file again. 因此,为了始终将文本行添加为第二行,您应该加载数据,在正确的位置添加行,然后再次写入文件。

But I think what you are doing is a very weird thing to do, and most likely avoidable. 但是我认为您正在做的事情很奇怪,而且很可能是可以避免的。 You are writing an HTML file which makes me think you are going to display it at some point, in which case you should separate data handling and presentation. 您正在编写一个HTML文件,这使我认为您将在某个时候显示它,在这种情况下,应将数据处理和表示分开。

For example you can treat the file as a data file where you just append lines without a header, then when you want to serve the data as html you output a header first and then the lines in reverse order. 例如,您可以将文件视为数据文件,在其中仅添加不带标题的行,然后以html格式提供数据时,首先输出标题,然后以相反的顺序输出行。

First of all, you should write the first line at the time you create the file, eg: 首先,您应该在创建文件时写第一行,例如:

$first_line = "<!DOCTYPE html><html><head>" . PHP_EOL;
file_put_contents("somefile.txt", $first_line);

And when you want to insert new content you could load your file into an array with file() and insert the content at index 1 of the array (which is the second line in the file) by using array_splice() : 当您想插入新内容时,可以使用file()file()加载到数组中,然后使用array_splice()将内容插入数组的索引1(文件的第二行array_splice()

$lines = file("somefile.txt");
array_splice($lines, 1, 0, "new content"); 

Use implode() to join the array elements to a string and finally write to the file: 使用implode()将数组元素连接到字符串,最后写入文件:

$file_content = implode(PHP_EOL, $lines);
file_put_contents("somefile.txt", $file_content);

Note that this approach will lead to memory issues when dealing with large files. 请注意,这种方法在处理大文件时会导致内存问题。

I ended up doing it this way, 我最终还是这样

<?php
$f=fopen('WriteFile.php', 'r+');
$W='<!DOCTYPE html>'.file_get_contents('WriteFile.php');
$S='New Data';
fwrite($f, "$W");
fgets($f);
fseek($f,15);
fwrite($f, "$S");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM