-1

So, my code is as follows:

$data = file_get_contents("js/plugins.js");

if (isset($_GET['resx'])) {
  if (isset($_GET['resy'])) {
  $data = str_replace('"Screen Width":"1366","Screen Height":"768"', '"Screen Width":"$resx","Screen Height":"$resy"', $data);
  }
}

file_put_contents("js/web_settings.js", $data);

What I am trying to do is define multiple variables in the URL bar and the website will update the file as such. Apparently, it seems to be replacing the string with the variable names itself instead of replacing it with the value of those variables. (So, if resx is 1280, 1366 becomes $resx instead of 1280) Due to the way the game engine works, the quotes need to be there. How do I fix this so it replaces it with the value of those variables?

user245115
  • 129
  • 5
  • http://php.net/manual/en/language.types.string.php - variables in single quotes are not parsed – rjdown Apr 17 '16 at 19:29
  • 2
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – chris85 Apr 17 '16 at 19:30

2 Answers2

1

Replace " to \" and ' to ". It will allow to use variables in the string.

$data = str_replace("\"Screen Width\":\"1366\",\"Screen Height\":\"768\"", "\"Screen Width\":\"$resx\",\"Screen Height\":\"$resy\"", $data);

Empty replace because $resx and $resy not exist:

$data = str_replace('"Screen Width":"1366","Screen Height":"768"', '"Screen Width":"'.$_GET['resx'].'","Screen Height":"'.$_GET['resy'].'"', $data);

Or make $resx = $_GET['resx']; before replace. If there was error output is enabled , you would immediately notice it.

jekaby
  • 403
  • 3
  • 13
  • The variables appear blank in the file. – user245115 Apr 17 '16 at 19:37
  • @user245115 I spoke about `$resx` `$resy` in your code – jekaby Apr 17 '16 at 19:40
  • Yes, but when they are updated in the file, the variables don't show up, so I get "Screen Width":"","Screen Height":"" – user245115 Apr 17 '16 at 19:43
  • May be try this? `$data = str_replace('"Screen Width":"1366","Screen Height":"768"', '"Screen Width":"'.$_GET['resx'].'","Screen Height":"'.$_GET['resy'].'"', $data);` is resx and resy params not empty? – jekaby Apr 17 '16 at 19:48
0
'Screen Width":"1366","Screen Height":"768"', '"Screen Width":"' . $resx . '","Screen Height":"' . $resy.'"', $data

You need to use ' . To go back to variables: 'string' . $variable . 'String again';

Andreas
  • 23,610
  • 6
  • 30
  • 62