php - Search in txt file, sum +1 and write the file -
Good day guys,
I'm not really about a person who does PHP coding, That's why I am asking you.
I have the file in txt format and in that file I place a line with "count: (n)", where "(n)" can be a numeric value.
I need to search for counting: (n), (n) assume, submit it with 1 and save the file again.
So if I have to count: 10 this should be 10 + 1 = 11.
Thank you! You might want to use some Reggae to parse 'count: n' string from the file Good luck!
$ file = fopen ('text.txt', 'r +'); // Open the file to read and write the file. $ FileContents = file_get_contents ($ file); // Load $ $ variableContents to the contents of the file $ CountString = preg_match ('/ Count: [0- 9] + /', $ fileContents); // See examples of string 'count: n' where n is an integer, load string in $ countString $ count = preg_match ('/ [0-9] + /', $ countString); // $ get the integer from countString, load $ count in $ count ++; // Equate one count up to $ NewCountString = 'Count:'. $ Count; // 'Count: n + 1' string where n is the original integer $ NewFileContents = preg_replace ('/ calculation: [0- 9] + /', $ newCountString, $ fileContents); // find string 'count: n' and replace with 'count: n + 1' where n is the original integer. Filitt ($ file, $ newFileContents); // Write new content in the file fclose ($ file);
Comments
Post a Comment