NuMan Posted August 25, 2008 Share Posted August 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/ Share on other sites More sharing options...
kenrbnsn Posted August 25, 2008 Share Posted August 25, 2008 Read the file into an array using the file() function Replace the line in the array Write the file back out using a combination of the put_file_contents() and implode() functions. Ken Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624942 Share on other sites More sharing options...
The Little Guy Posted August 25, 2008 Share Posted August 25, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624948 Share on other sites More sharing options...
NuMan Posted August 25, 2008 Author Share Posted August 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624973 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 Try: <?php $file = file_get_contents('test.txt'); $file = str_replace("line2\n", "New line!\n", $file); file_put_contents('test.txt', $file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624979 Share on other sites More sharing options...
NuMan Posted August 25, 2008 Author Share Posted August 25, 2008 Try: <?php $file = file_get_contents('test.txt'); $file = str_replace("line2\n", "New line!\n", $file); file_put_contents('test.txt', $file); ?> That doesn't work either it didn't change anything :| Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624990 Share on other sites More sharing options...
kenrbnsn Posted August 25, 2008 Share Posted August 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-624995 Share on other sites More sharing options...
NuMan Posted August 25, 2008 Author Share Posted August 25, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625007 Share on other sites More sharing options...
The Little Guy Posted August 25, 2008 Share Posted August 25, 2008 Who's method did you use? Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625016 Share on other sites More sharing options...
NuMan Posted August 25, 2008 Author Share Posted August 25, 2008 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625121 Share on other sites More sharing options...
NuMan Posted August 25, 2008 Author Share Posted August 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625481 Share on other sites More sharing options...
The Little Guy Posted August 26, 2008 Share Posted August 26, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625614 Share on other sites More sharing options...
NuMan Posted August 26, 2008 Author Share Posted August 26, 2008 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 " " Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-625842 Share on other sites More sharing options...
NuMan Posted August 27, 2008 Author Share Posted August 27, 2008 bump.... any help? Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-626852 Share on other sites More sharing options...
NuMan Posted January 3, 2009 Author Share Posted January 3, 2009 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.! Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728542 Share on other sites More sharing options...
kenrbnsn Posted January 3, 2009 Share Posted January 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728546 Share on other sites More sharing options...
NuMan Posted January 3, 2009 Author Share Posted January 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728787 Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728788 Share on other sites More sharing options...
NuMan Posted January 3, 2009 Author Share Posted January 3, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728793 Share on other sites More sharing options...
premiso Posted January 3, 2009 Share Posted January 3, 2009 <?php $data = file('variables.php'); $data[1] = '$name = '.$_POST['info'] ." \r\n"; // entry 1 is the 2nd line file_put_contents('variables.php',$data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728798 Share on other sites More sharing options...
NuMan Posted January 3, 2009 Author Share Posted January 3, 2009 <?php $data = file('variables.php'); $data[1] = '$name = '.$_POST['info'] ." \r\n"; // entry 1 is the 2nd line file_put_contents('variables.php',$data); ?> Thanks a lot, that solved my problem Quote Link to comment https://forums.phpfreaks.com/topic/121229-replacing-a-line-inside-file/#findComment-728805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.