Help replacing a whole line in file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

xNuManx
Forum Newbie
Posts: 13
Joined: Wed Aug 27, 2008 9:17 am

Re: Help replacing a whole line in file

Post by xNuManx »

i do have them i just wrote that manually.

I tried this:

Code: Select all

 
<?php
$new = $_POST['newname'];
echo "$new <br /><br />";
$file = file_get_contents('variables.php');
echo "$file <br /><br />";
$file = preg_replace("/\$name = '.*?';/i", "\$name = '$new';", $file);
echo "$file <br /><br />";
file_put_contents('variables.php', $file);
?>
 
I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Help replacing a whole line in file

Post by Chalks »

xNuManx wrote:I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
it didn't echo anything else? If not, then it's not opening the file variables.php correctly (or variables.php is empty). Make sure it's in the same directory and everything.
xNuManx
Forum Newbie
Posts: 13
Joined: Wed Aug 27, 2008 9:17 am

Re: Help replacing a whole line in file

Post by xNuManx »

Chalks wrote:
xNuManx wrote:I put new in the input box, then ran the script and it echoed new. but the file is still unchanged.
it didn't echo anything else? If not, then it's not opening the file variables.php correctly (or variables.php is empty). Make sure it's in the same directory and everything.
When i put wrong directory it gives me this error:
Warning: file_get_contents(includes/variables.php) [function.file-get-contents]: failed to open stream: No such file or directory in name_do.php on line 4

so i know its fine :(. Is this the only method there is to do this?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Help replacing a whole line in file

Post by Chalks »

This is the easiest way to do it.

are you sure it _only_ echoed "new"?
xNuManx
Forum Newbie
Posts: 13
Joined: Wed Aug 27, 2008 9:17 am

Re: Help replacing a whole line in file

Post by xNuManx »

Chalks wrote:This is the easiest way to do it.

are you sure it _only_ echoed "new"?
Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Help replacing a whole line in file

Post by Chalks »

xNuManx wrote:Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
Variables.php is empty, or file_get_contents isn't actually getting the content. I suggest opening the file and checking. Unfortunately, I'm leaving work now, so I won't be able to help any further until much later tonight. I've tested the code I gave you though, and as far as I can tell, it should work just fine.
xNuManx
Forum Newbie
Posts: 13
Joined: Wed Aug 27, 2008 9:17 am

Re: Help replacing a whole line in file

Post by xNuManx »

Chalks wrote:
xNuManx wrote:Yes, im pretty sure. I tried it again and only echoed New then i input hi and it echoed hi. Any suggestions?
Variables.php is empty, or file_get_contents isn't actually getting the content. I suggest opening the file and checking. Unfortunately, I'm leaving work now, so I won't be able to help any further until much later tonight. I've tested the code I gave you though, and as far as I can tell, it should work just fine.
Weird, is not empty. Well thanks for trying anyways :D
Post Reply