Code: Select all
$string1 = 'string1'
$string2 = 'string2'
$string3 = 'string3'
Moderator: General Moderators
Code: Select all
$string1 = 'string1'
$string2 = 'string2'
$string3 = 'string3'
Code: Select all
$string2 = "new values";How about variables inside of filesChalks wrote:if you're just dealing with variables (i.e. $string2), then simplyif you're dealing with files, look at fwrite()Code: Select all
$string2 = "new values";
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);
?>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.xNuManx wrote:How about variables inside of files
Code: Select all
error_reporting(E_ALL);Actually, now that I look at it closer, your problem is probable with line 4. It is trying to replace the stringxNuManx 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); ?>
i get this:Chalks wrote: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.xNuManx wrote:How about variables inside of files
Edit: beat me to it, give me a second to read your post.
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);
$name doesn't exist. If you are looking for the string "$name", not the variable $name, do this:xNuManx wrote:Notice: Undefined variable: name in C:\wamp\www\includes\admin_do.php on line 5
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 workChalks wrote:$name doesn't exist. If you are looking for the string "$name", not the variable $name, do this:xNuManx wrote:Notice: Undefined variable: name in C:\wamp\www\includes\admin_do.php on line 5Code: Select all
$file = str_replace("\$name = \r\n", "$new\r\n", $file);
Here's what is happening. str_replace will search the entire text for this exact string (using the code I gave you):xNuManx wrote:That doesn't show me any errors but it also didn't worki just want to replace line 2 with a new line.
That worked just fine, thanks a lot. Only 1 problem. This is my variables.phpChalks wrote:Here's what is happening. str_replace will search the entire text for this exact string (using the code I gave you):xNuManx wrote:That doesn't show me any errors but it also didn't worki just want to replace line 2 with a new line.
"$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.
Code: Select all
<?php
$line1 = '1';
$name = 'name';
$line2 = '2';
?>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);
?>
Try this. The regex is untested however, and I'm a regex noob, so it might not work first try.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?
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 :/Chalks wrote:Try this. The regex is untested however, and I'm a regex noob, so it might not work first try.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?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); ?>
More feedback than that is needed. What doesn't work about it? Is the file changed? If yes, what is it changed to?xNuManx wrote:Neither shows errors, or works :/
It doesn't change anything inside the file or gives any errors.Chalks wrote:More feedback than that is needed. What doesn't work about it? Is the file changed? If yes, what is it changed to?xNuManx wrote:Neither shows errors, or works :/
I do see an error I made though, replace the 4th line with:
$file = preg_replace("/\$name = '.*?';/i", "\$name = '$new';", $file);
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:xNuManx wrote:It doesn't change anything inside the file or gives any errors.
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);
?>