Jump to content

Insert text to particular position in a file


ashly123

Recommended Posts

Hi friends,

 

I am a newbie in string functions in php. I have a text file which contains some lines of text in a particular format.

I am using php to maniputlate this text file.

 

Text1 = This is my first line of text
Text2 = This is my second text
Text3 = This is my third text
Text5 = This is fifth line of text
Text10 = This is tenth line in the file
....
....

 

I would like to insert some missing text at the end or in between the existing lines. For example, I would like to write Text7 in the file in the exact place now. Can anyone please give me a solution for this?

 

After the insertion, the file should look like this:

 

Text1 = This is my first line of text
Text2 = This is my second text
Text3 = This is my third text
Text5 = This is fifth line of text
Text7 = This is seventh line in the file
Text10 = This is tenth line in the file
....
....

 

Link to comment
Share on other sites

You will need to read, and rewrite the file with the new line if you are inserting it. If you are appending, you just need to use "A+" in fwrite.

 

To Append:

$file="myFile.txt";

$handle=fopen($file, "A+"); // Open for appending.

fwrite($handle, "Myline");

 

To Insert:

$lineNumber=7;

$myLine="Insert this text.";

$newWrite="";

$insert=0;

$contents=file_get_contents("MyFile.txt");

 

$lines=explode("\r\n", $contents);

foreach ($lines as $line) {

$values=explode("=", $line);

$val=trim(substr($values[0], 0, 4));

if ($val > $lineNumber && $insert==0) {

  $insert=1;

  $newWrite.="Text{$lineNumber} = {$myLine}\r\n";

}

$newWrite.=$line."\r\n";

}

$handle=fopen($file, "w+");

fwrite($handle, $newWrite);

fclose($handle);

Link to comment
Share on other sites

or...

There are several ways to handle this. Structuring your file properly initially is the best approach.

 

ie it appears that you want the lines to ultimately be in sequential order; sooo...

one approach would be to build your file with blank lines ie

 

Text0 = This is my first line of text

Text1 = This is my second text

Text2 = This is my third text

BLANK LINE HERE

Text4 = This is fifth line of text

BLANK LINE HERE

BLANK LINE HERE

BLANK LINE HERE

BLANK LINE HERE

Text9 = This is tenth line in the file

Text10 = This is eleventh line in the file

 

remember with arrays the count starts with 0 being line 1

then

 

<?PHP
$new_line = "Text7 = This is eighth line in the file";
$element_number_to_replace = 7; /* remember the count starts with 0 being line 1 */
$line_array = file($my_text_file);
/*
what your file/ array looks like initially

Text0 = This is my first line of text
Text1 = This is my second text
Text2 = This is my third text
BLANK LINE HERE
Text4 = This is fifth line of text
BLANK LINE HERE
BLANK LINE HERE
BLANK LINE HERE
BLANK LINE HERE
Text9 = This is tenth line in the file
Text10 = This is eleventh line in the file
*/

$line_number_to_insert = 7;
$line_array[$line_number_to_insert] = $new_line;
$all_content = implode("\n", $line_array);
file_put_contents($my_test_file,$all_content);
/*
what your file/ array looks like after

Text0 = This is my first line of text
Text1 = This is my second text
Text2 = This is my third text
BLANK LINE HERE
Text4 = This is fifth line of text
BLANK LINE HERE
BLANK LINE HERE
Text7 = This is eighth line in the file
BLANK LINE HERE
Text9 = This is tenth line in the file
Text10 = This is eleventh line in the file
*7


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.