christa Posted September 7, 2007 Share Posted September 7, 2007 Hi I have a txt file as this: [section1] parameter1:2345|parameter2:goal|parameter3:other [section2] parameter1:444|parameter2:val|parameter3:gold Now, i have to edit only one section (section1 or section2 or sectionN...) and edit the values of parameters via form. Then, update the file with new values. The values are (in the above example): 2345, goal, other.... How can I do? please, also post a few of code and example, because i'm a newbie of php. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/68352-modify-a-text-file/ Share on other sites More sharing options...
christa Posted September 7, 2007 Author Share Posted September 7, 2007 someone can help me, please? Quote Link to comment https://forums.phpfreaks.com/topic/68352-modify-a-text-file/#findComment-343860 Share on other sites More sharing options...
effigy Posted September 7, 2007 Share Posted September 7, 2007 Open the file and read it line by line to place it in a new structure: <pre> <?php $data = <<<DATA [section1] parameter1:2345|parameter2:goal|parameter3:other [section2] parameter1:444|parameter2:val|parameter3:gold DATA; $lines = explode("\n", $data); $section = 'none'; foreach ($lines as $line) { if (preg_match('/\[([^]]+)\]/', $line, $matches)) { $section = $matches[1]; } if (strpos($line, 'parameter') !== FALSE) { $array[$section] = preg_split('/parameter\d+:|\|/', $line, -1, PREG_SPLIT_NO_EMPTY); } } print_r($array); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/68352-modify-a-text-file/#findComment-343877 Share on other sites More sharing options...
christa Posted September 7, 2007 Author Share Posted September 7, 2007 thanks. But i've edit your code in order to obtain this (view image): I use this code: <form> <?php $filename = "file.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); $lines = explode("\n", $contents); $section = 'none'; foreach ($lines as $line) { if (preg_match('/\[([^]]+)\]/', $line, $matches)) { $section = $matches[1]; echo "<input type='text' name='$section' value=$section><br />"; } if (strpos($line, 'parameter') !== FALSE) { $array[$section] = preg_split('/parameter\d+:|\|/', $line, -1, PREG_SPLIT_NO_EMPTY); echo "<input type='text' name='$line' value=$array><br />"; } } //print_r($array); fclose($handle); ?> </form> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/68352-modify-a-text-file/#findComment-343912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.