Ok.. so suppose you had a text file like this:
name
code1
code2
code1 and code2 can be whatever...
so in php, you tell it what file and where it is, then use file() to extract the contents as an array... manipulate say name (first entry in file), then implode back into string and save / close.. so say in your phpo file, you have a variable $name that you want to put into the fie...
Example:
$name = 'Johnnie';
$file_ini = $_SERVER['DOCUMENT_ROOT'].'/whatever folder/whatever_file.ini';
if(file_exists($file_ini)){
$fileContents = file($file_ini);
$fileContents[0] = $name . "\r\n"; // this is the name you want to replace..
$str = implode('', $fileContents);
file_put_contents($file_ini, $str);
unset($fileContents, $str);
} else {
echo "Error! The file you are looking for does not exist!";
exit;
}
Now I have had to manipulate all array key values in my project, so I overwrote each key one at a time, ensuring I added "\r\n" at the end of each, then the rest is the same (implode and save).
Granted, I suppose instead of going the array route, you could always have your file as a string with not just the variable info, but some additional info that is just there to help regex find it..
for example:
name: NULL codeOne: code1 codeTwo: code2
then access the file (instead of file(), simply use file_get_contents() to load contents as a string. From there, you simply use regex to replace a portion of it, then simply save and close it via file_put_contents().
example:
$name = 'Johnnie'; //name to insert into file...
$file_ini = $_SERVER['DOCUMENT_ROOT'].'/whatever folder/whatever_file.ini';
if(file_exists($file_ini)){
$str = file_get_contents($file_ini);
$str = preg_replace('#(?<=name:\s)(\w+)#', "$name", $str);
file_put_contents($file_ini, $str);
} else {
echo "Error! The file you are looking for does not exist!";
exit;
}
Just a few assumptions with the regex patter..
the space in the look behind assertion (\s) actually can be many whitespaces, such as space, tab, etc.. if you need to explicitly list a space character only, you can use '\x20' instead. The group for capture (\w+) assumes the characters in the name is only comprised of [a-zA-Z0-9_].. so if more characters is needed, then use a single character class than encompasses what you need.
So going the regex route on a string, you don't have to fuss with any new lines or the like.. If you want to make either of those code snippets fully useful, just have them as a function which when called, loops through all the variables in the file and overwrite them all in one fell swoop.
Sorry for the long post. Hope this helped out.