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
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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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