Page 1 of 2

Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:20 am
by xNuManx
Hello i want to replace a single line in a file for example:

Code: Select all

 
$string1 = 'string1'
$string2 = 'string2'
$string3 = 'string3'
 
What i want to do is replace the whole line 2 or just replace whatever is inside ' ' in, no matter what it was.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:22 am
by Chalks
if you're just dealing with variables (i.e. $string2), then simply

Code: Select all

$string2 = "new values";
if you're dealing with files, look at fwrite()

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:25 am
by xNuManx
Chalks wrote:if you're just dealing with variables (i.e. $string2), then simply

Code: Select all

$string2 = "new values";
if you're dealing with files, look at fwrite()
How about variables inside of files

I have this:

Code: Select all

 
<?php
$new = $_POST['newname'];
$file = file_get_contents('variables.php');
$file = str_replace("$name = \r\n", "$new\r\n", $file);
file_put_contents('variables.php', $file);
?>
But it doesn't work :(.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:28 am
by Chalks
xNuManx wrote:How about variables inside of files :oops:
At this point, I'm not really sure what you're asking. How 'bout you post a bit of your code, and what you're trying to do, and we'll go from there.

Edit: beat me to it, give me a second to read your post. :D

Edit2: What errors are showing? If there are no errors, try putting this at the top of your script:

Code: Select all

error_reporting(E_ALL);

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:37 am
by Chalks
xNuManx wrote:

Code: Select all

<?php
$new = $_POST['newname'];
$file = file_get_contents('variables.php');
$file = str_replace("$name = \r\n", "$new\r\n", $file);
file_put_contents('variables.php', $file);
?>
Actually, now that I look at it closer, your problem is probable with line 4. It is trying to replace the string

"filename =
"
if that _exact_ string doesn't exist, then the variable $new will never be put in there.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:38 am
by xNuManx
Chalks wrote:
xNuManx wrote:How about variables inside of files :oops:
At this point, I'm not really sure what you're asking. How 'bout you post a bit of your code, and what you're trying to do, and we'll go from there.

Edit: beat me to it, give me a second to read your post. :D

Edit2: What errors are showing? If there are no errors, try putting this at the top of your script:

Code: Select all

error_reporting(E_ALL);
i get this:
Notice: Undefined variable: name in C:\wamp\www\includes\admin_do.php on line 5

This is line 5
$file = str_replace("$name = \r\n", "$new\r\n", $file);

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:40 am
by Chalks
xNuManx wrote:Notice: Undefined variable: name in C:\wamp\www\includes\admin_do.php on line 5
$name doesn't exist. If you are looking for the string "$name", not the variable $name, do this:

Code: Select all

$file = str_replace("\$name = \r\n", "$new\r\n", $file);

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:45 am
by xNuManx
Chalks wrote:
xNuManx wrote:Notice: Undefined variable: name in C:\wamp\www\includes\admin_do.php on line 5
$name doesn't exist. If you are looking for the string "$name", not the variable $name, do this:

Code: Select all

$file = str_replace("\$name = \r\n", "$new\r\n", $file);
That doesn't show me any errors but it also didn't work :( i just want to replace line 2 with a new line.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 9:52 am
by Chalks
xNuManx wrote:That doesn't show me any errors but it also didn't work :( i just want to replace line 2 with a new line.
Here's what is happening. str_replace will search the entire text for this exact string (using the code I gave you):

"$name =
"

your code will search for

"ValueOfNameVariable =
"

my guess is that you're trying to replace an old name with a new name. you need to do this:
$file = str_replace("nameYouWantReplaced", "$new", $file);

if that also doesn't work, you'll need to copy the relevant portion of the file that you're trying to replace and show it here.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:00 am
by xNuManx
Chalks wrote:
xNuManx wrote:That doesn't show me any errors but it also didn't work :( i just want to replace line 2 with a new line.
Here's what is happening. str_replace will search the entire text for this exact string (using the code I gave you):

"$name =
"

your code will search for

"ValueOfNameVariable =
"

my guess is that you're trying to replace an old name with a new name. you need to do this:
$file = str_replace("nameYouWantReplaced", "$new", $file);

if that also doesn't work, you'll need to copy the relevant portion of the file that you're trying to replace and show it here.
That worked just fine, thanks a lot. Only 1 problem. This is my variables.php

Code: Select all

<?php
$line1 = '1';
$name = 'name';
$line2 = '2';
?>
And this is the code im using:

Code: Select all

 
<?php
$new = $_POST['newname'];
$file = file_get_contents('variables.php');
$file = str_replace("'name'", "'$new'", $file);
file_put_contents('variables.php', $file);
?>
 
Now the problem is that after the 1st time i run this, it won't work because the str_replace("'name'") won't be it since it'll be changed which is why i wanted to be able to edit the whole line. Anything i could do about that?

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:21 am
by Chalks
xNuManx wrote:Now the problem is that after the 1st time i run this, it won't work because the str_replace("'name'") won't be it since it'll be changed which is why i wanted to be able to edit the whole line. Anything i could do about that?
Try this. The regex is untested however, and I'm a regex noob, so it might not work first try.

Code: Select all

 <?php
$new = $_POST['newname'];
$file = file_get_contents('variables.php');
$file = preg_replace("/\$name = '.*?';/i", "$new", $file);
file_put_contents('variables.php', $file);
?>

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:31 am
by xNuManx
Chalks wrote:
xNuManx wrote:Now the problem is that after the 1st time i run this, it won't work because the str_replace("'name'") won't be it since it'll be changed which is why i wanted to be able to edit the whole line. Anything i could do about that?
Try this. The regex is untested however, and I'm a regex noob, so it might not work first try.

Code: Select all

 <?php
$new = $_POST['newname'];
$file = file_get_contents('variables.php');
$file = preg_replace("/\$name = '.*?';/i", "$new", $file);
file_put_contents('variables.php', $file);
?>
Neither shows errors, or works :/

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:34 am
by Chalks
xNuManx wrote:Neither shows errors, or works :/
More feedback than that is needed. What doesn't work about it? Is the file changed? If yes, what is it changed to?

I do see an error I made though, replace the 4th line with:
$file = preg_replace("/\$name = '.*?';/i", "\$name = '$new';", $file);

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:38 am
by xNuManx
Chalks wrote:
xNuManx wrote:Neither shows errors, or works :/
More feedback than that is needed. What doesn't work about it? Is the file changed? If yes, what is it changed to?

I do see an error I made though, replace the 4th line with:
$file = preg_replace("/\$name = '.*?';/i", "\$name = '$new';", $file);
It doesn't change anything inside the file or gives any errors.

Re: Help replacing a whole line in file

Posted: Wed Aug 27, 2008 10:46 am
by Chalks
xNuManx wrote:It doesn't change anything inside the file or gives any errors.
Ok, I just tested the regex, and it is correct. I also double checked the preg_replace syntax, and it is correct as well. So, for more error checking, do 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);
?>
 
 
 
and show me what results. Edit, I don't know why this is red.

EDIT: Wait a minute, how come you don't have semi-colons after each variable in variables.php? That's what is throwing off the regex.