I'm trying to update line 65 in a Java file called Activity.java
using this code:
$arr = file(getcwd() . "/details/Activity.java");
$arr[64] = 'public static String name = "hello world";';
file_put_contents($getcwd() . "/details/Activity.java", implode($arr));
This file supposed to replace line 65 in Activity.java
with public static String name = "hello world";
.
What really happens, is that it is replacing the existing text, but removes the line break, so it shows like this:
public static String name = "hello world"; public static String address = "address";
So, as you can understand, it removes the line break between lines 65 an 66, so lines 65 and 66 will be on the same line, and then continues to line 67 straight below line 65.
I tried to write it like this:
$arr[64] = 'public static String name = "hello world"; \n';
But then the \n
was just written on the same line between the texts from lines 65 and 66, like this:
public static String name = "hello world"; \n public static String address = "address";
How can I fix this to enter a line break between line 65 and 66?
try adding a newline character to the end of your new string.
$arr = file(getcwd() . "/details/Activity.java");
$arr[64] = 'public static String name = "hello world";'. "\n";
file_put_contents($getcwd() . "/details/Activity.java", implode($arr));