Jump to content

Replacing a line inside file


NuMan

Recommended Posts

I've been searching over and over and i can't seem to find the function to do it.

What i want to do is replace a specific line inside a txt file for example:

 

line1
line2
line3

 

change line2 in that file to another line that i'd want. Any help will be greatly appreciated  :)

 

Link to comment
Share on other sites

This might work, but I have not tested it, so give it a try:

 

// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "w+");
$contents = fread($handle, filesize($filename));

$cArray = explode("\n",$contents);

$line = 2 - 1;

$cArray[$line] = "New Line Text";

$newFile = implode("\n",$cArray);

$fwrite($handle, $newFile);

fclose($handle);

Link to comment
Share on other sites

This might work, but I have not tested it, so give it a try:

 

// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "w+");
$contents = fread($handle, filesize($filename));

$cArray = explode("\n",$contents);

$line = 2 - 1;

$cArray[$line] = "New Line Text";

$newFile = implode("\n",$cArray);

$fwrite($handle, $newFile);

fclose($handle);

 

Tried this to test:

<?
$filename = "text.txt";
$handle = fopen($filename, "w+");
$contents = fread($handle, filesize($filename));

$cArray = explode("\n",$contents);

$line = 2;

$cArray[$line] = "New Line Text";

$newFile = implode("\n",$cArray);

$fwrite($handle, $newFile);

fclose($handle);?>

 

Didn't work :( my text.txt is only this:

line1
line2
line3

Link to comment
Share on other sites

What type of OS are your running? Linux or Windows? If you're running Windows you need to use "\r\n" instead of "\n" for the line terminator.

 

Ken

 

I am running windows o.o; let me try that EDIT: worked fine thanks :) problem solved!

Link to comment
Share on other sites

Who's method did you use?

 

DarkWater's im trying yours with \r\

 

Edit: tried yours and it emptyed out the file, then showed this :P

 

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\wamp\www\test.php on line 5

 

Fatal error: Function name must be a string in C:\wamp\www\test.php on line 15

Link to comment
Share on other sites

sorry to bump, but those anyone knows how to replace a string withing a string? like the fallowing:

 

$line1
$line2 = 'string'
$line3

 

I want to replace 'string' inside of line no matter what it was too OR just replace the whole line with a new one.. Help please :)

Link to comment
Share on other sites

Try this:

 

$filename = "/usr/local/something.txt";
$handle = fopen($filename, "w+");
$contents = fread($handle, filesize($filename));

$findSTR = 'string';
$replaceSTR = 'new string';

preg_replace("~$findSTR~", $replaceSTR, $contents);

fwrite($handle, $contents);
fclose($handle);

 

But that only replaces the text string :/ i want to replace the whole line. Or the text between the " "

Link to comment
Share on other sites

  • 4 months later...

Hi, i am rebumping this old topic because i still need help with this :(

 

want a code that will edit a specified line in a php file no matter what the info in it is.

 

For example lets say my file has the following info:

<?php
$var1 = something1;
$var2 = something2;
$var3 = something3;
?>

 

I want to be able to edit line 2 ($var2 = something2;) to something else from POST, not mattering what line 2 contents.

 

Thanks in advanced.!

Link to comment
Share on other sites

This is much easier:

<?php
$data = file('input_file.txt');
$data[1] = $_POST['data'] . "\n"; // entry 1 is the 2nd line
file_put_contents('input_file.txt',$data);
?>

 

Ken

 

Thank you that did work. One more thing though. I tried this:

<?php
$data = file('variables.php');
$data[1] = "$name = ".$_POST['info'] ." "\r\n"; // entry 1 is the 2nd line
file_put_contents('variables.php',$data);
?>

 

And i get this error :

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING  on line 3

What i am trying to do its get it to write:

$var = "$_POST['info']";

 

Thanks again.

Link to comment
Share on other sites


<?php
$data = file('variables.php');
$data[1] = "$name = ".$_POST['info'] ." \r\n"; // entry 1 is the 2nd line
file_put_contents('variables.php',$data);
?>

 

That should work.

 

Hi, I tried that and this is what it writes:

<?php
= (info from $_POST['info'])
?>

 

What it should write is:

<?php
$name = "(info from $_POST['info'])";
?>

EDIT: I got it to type:

 = "(info from $post)"

by changing to:

$data[1] = "$name = \"".$_POST['info'] ."\" \r\n";

 

But i still can't get $name = to show.

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.