Mindaugas Posted January 6, 2010 Share Posted January 6, 2010 Hello all, i know that database is better with MySQL , but i am working with my first simple project, and using .txt datadase. And i won`t give up till this project will be done, then i`ll learn MySQL. This is the question, is there any way to rewrite data to .txt file without overwriting all variables ? for example, i`m creating simple game, and when player buying for example a weapon, his atk should add +x, and i have to overwrite all variables like this. //deleting old info $filees = fopen("krepsys/$vartotojas.txt","w"); fclose($filees); //rewriting variables and weapons power $inf2[3] = $weaponsPower; $duoma = $inf2[0] . ".." . $inf2[1] . ".." . $inf2[2] . ".." . $inf2[3] . ".." . $inf2[4] . ".." . $inf2[5] . ".." . $inf2[6] . ".." . $inf2[7] . ".." . $inf2[8] . ".." . $inf2[9] . ".." . $inf2[10] . ".." . $inf2[11] . ".." . $inf2[12] . ".." . $inf2[13] . ".." . $inf2[14] . ".." . $inf2[15] . ".." . $inf2[16] . ".." . $inf2[17] . ".." . $inf2[18] . ".." . $inf2[19] . ".." . $inf2[20] . ".." . $inf2[21] . ".." . $inf2[22] . ".." . $inf2[23] . ".." . $inf2[24] . ".." . $inf2[25] . ".." . $inf2[26] . ".." . $inf2[27] . ".." . $inf2[28] . ".." . $inf2[29] . ".." . $inf2[28] . ".." . $inf2[29] . ".." . $inf2[30] . ".." . $inf2[31] . ".." . $inf2[32] . ".." . $inf2[33] . ".." . $inf2[34] . ".." . $inf2[35] . ".." . $inf2[36] . ".." . $inf2[37] . ".." . $inf2[38] . ".." . $inf2[39] . ".." . $inf2[40] . ".." . $inf2[41] . ".." . $inf2[42] . ".." . $inf2[43] . ".." . $inf2[44] . ".." . $inf2[45] . ".." . $inf2[46] . ".." . $inf2[47] . ".." . $inf2[48] . ".." . $inf2[49] . ".." . $inf2[50] . ".." . $inf2[51] . ".." . $inf2[52] . ".." . $inf2[53] . ".." . $inf2[54] . ".." . $inf2[55] . ".." . $inf2[56] . ".." . $inf2[57] . ".." . $inf2[58] . ".." . $inf2[59]; $failas11 = fopen("krepsys/$vartotojas.txt","a"); fwrite($failas11, "$duoma"); fclose($failas11); Is any other way to overwrite only one file ? I tried use only one variable, but then all others becoming empty Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/ Share on other sites More sharing options...
premiso Posted January 6, 2010 Share Posted January 6, 2010 You could possibly read in the file to a string using file_get_contents then use preg_replace to replace what you need to replace and write the string back to the file. Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-989897 Share on other sites More sharing options...
Mindaugas Posted January 6, 2010 Author Share Posted January 6, 2010 Thx for quick answer Like this ? $string = file_get_contents("file.txt"); $pattern = '$weaponPower'; $newWeaponPower = $buyght_item_power; $replacement = '$newWeaponPower'; preg_replace($patterns, $replacements, $string); Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-989906 Share on other sites More sharing options...
premiso Posted January 6, 2010 Share Posted January 6, 2010 You may have to have a pattern, if that is the case I would suggest posting it in the REGEX forum with an example line from the file and highlighting the section that you would like replaced. Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-989914 Share on other sites More sharing options...
Mindaugas Posted January 6, 2010 Author Share Posted January 6, 2010 Thx I`ll try Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-989919 Share on other sites More sharing options...
teamatomic Posted January 6, 2010 Share Posted January 6, 2010 Seeing as the data is known to you, $string = file_get_contents("file.txt"); list($a,$b,$c,$d)=explode("..","$string"); $b=$weaponsPower; $NewString="$a..$b..$c..$d"; file_put_contents("$NewString","file.txt"); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-989955 Share on other sites More sharing options...
Mindaugas Posted January 7, 2010 Author Share Posted January 7, 2010 Seeing as the data is known to you, $string = file_get_contents("file.txt"); list($a,$b,$c,$d)=explode("..","$string"); $b=$weaponsPower; $NewString="$a..$b..$c..$d"; file_put_contents("$NewString","file.txt"); HTH Teamatomic thanks a lot Link to comment https://forums.phpfreaks.com/topic/187472-rewriting-data-to-txt-file/#findComment-990566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.