aQ Posted July 25, 2007 Share Posted July 25, 2007 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 More sharing options...
ToonMariner Posted July 25, 2007 Share Posted July 25, 2007 $data = explode('#',$str); $tmp = $data[2]; $data[2] = $data[count($data) -1]; $data[count($data) -1] = $tmp; Link to comment https://forums.phpfreaks.com/topic/61691-solved-change-positions-in-file/#findComment-307071 Share on other sites More sharing options...
aQ Posted July 25, 2007 Author Share Posted July 25, 2007 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 https://forums.phpfreaks.com/topic/61691-solved-change-positions-in-file/#findComment-307196 Share on other sites More sharing options...
ToonMariner Posted July 25, 2007 Share Posted July 25, 2007 why did it not work???? apart from the delimiter I used ('#' - which you should have changed to what ever you use) then the code i gave would swap the info in the 3rd element with the last one.... Link to comment https://forums.phpfreaks.com/topic/61691-solved-change-positions-in-file/#findComment-307483 Share on other sites More sharing options...
aQ Posted July 26, 2007 Author Share Posted July 26, 2007 It worked when I re-wrote the code, it was pretty much the same, but I'm not sure what did it. The best thing is that it now works. Thanks. Link to comment https://forums.phpfreaks.com/topic/61691-solved-change-positions-in-file/#findComment-307881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.