Jump to content

[SOLVED] Change positions in file


aQ

Recommended Posts

Hello!

 

I am trying to move change place of two values of a variable, but it won't work.

$var = "";
$var .= "value---";
$var .= "anotherValue---";
$var .= "thirdValue---";
$var .= "andTheLastOne---";

 

This is what I've got. I use the explode function to divide the variable into an array. Then I want to move "thirdValue---" to "andTheLastOne---", and "andTheLastOne---" to "thirdValue---". Basically to change the values' positions. I have tried using str_replace, but when I replace andTheLastOne--- with thirdValue---, I don't know where to put in andTheLastOne---, I get two thirdValue---, and no andTheLastOne---. You see?

 

It would be great if someone could help me.

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/61691-solved-change-positions-in-file/
Share on other sites

It won't work. Here is my code:

 

 

			$file = file_get_contents("file.php");
		$mods = explode("---", $var);
		print "<textarea rows=50 cols=50>".$file."</textarea>";
		$totalmods = count($mods)-3;
		$find = $_POST[move2];
		$replace = $_POST[move1];
		$i = -1;
		while($i++ <= $totalmods){
			if($mods[$i] == $_POST[move1]){
				print "<p>$_POST[move1]<p>";
				$rmod = str_replace('$var .= "'.$mods[$i].'---";', '$var .= "'.$find.'---";', $file);
			}
			if($mods[$i] == $_POST[move2]){
				print "<p>$_POST[move2]<p>";
				$rmod = str_replace('$var .= "'.$mods[$i].'---";', '$var .= "'.$replace.'---";', $file);
			}
		}
		print "<textarea rows=50 cols=50>".$rmod."</textarea>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.