erisoov Posted September 28, 2023 Share Posted September 28, 2023 (edited) have a text file with the following lines, I want to sort these lines by the numeric code, which appears after the date always in the same position and the same format i.e. 05 Then I want to add a total at the bottom for each numeric. Here is the data 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Nerja Malaga 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Cartagena Murcia 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Edmore Michigan 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Alhaurin de la Torre Malaga 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Alhaurin de la Torre Malaga 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 15/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Finland Helsinki Uusimaa 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Madrid Madrid 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 18/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Finland Helsinki Uusimaa 21/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 Norway Sira Agder 21/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Nerja Malaga 21/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Germany Gera Thuringia 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Garden City New York 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 Spain Muro del Alcoy Alicante 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Edmore Michigan 10/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 11/09/2023 20 The Legacy of Jesus (El Legado de Jesús)20-8-2023.mp3 United States Ashburn Virginia 15/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Finland Helsinki Uusimaa 18/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Finland Helsinki Uusimaa 21/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Germany Gera Thuringia This is the output Total Click Counts 03 Resist the Curse (Resiste la Maldición) Total clicks = 3 20 The Legacy of Jesus (El Legado de Jesús) Total clicks = 4 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) Total clicks = 19 19 The Bread of Life (El Pan de la Vida) Total clicks = 20 05 Day of Atonement 2023 (Día de la Expiación) Total clicks = 24 The current code I have is as follows <?php // Initialize an array to store the counts for each category $categoryCounts = []; // Open and read the text file $file = fopen('file.txt', 'r'); if ($file) { while (($line = fgets($file)) !== false) { // Extract the numeric code from the line if (preg_match('/\d{2}\/\d{2}\/\d{4} (\d{2})/', $line, $matches)) { $category = $matches[1]; if (!isset($categoryCounts[$category])) { $categoryCounts[$category] = 0; } $categoryCounts[$category]++; } } fclose($file); // Sort the lines based on the numeric code asort($categoryCounts); // Print the totals for each category foreach ($categoryCounts as $category => $count) { echo "Total for category $category = $count\n"; } } else { echo "Failed to open the file."; } ?> Which give me the following output Total Click Counts Total clicks for message number 06 = 1 Total clicks for message number 03 = 3 Total clicks for message number 20 = 7 Total clicks for message number 19 = 19 Total clicks for message number 04 = 19 Total clicks for message number 05 = 23 I want to add to the above code so that it gives the following output Total Click Counts 03 Resist the Curse (Resiste la Maldición) Total clicks = 3 20 The Legacy of Jesus (El Legado de Jesús) Total clicks = 4 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) Total clicks = 19 19 The Bread of Life (El Pan de la Vida) Total clicks = 20 05 Day of Atonement 2023 (Día de la Expiación) Total clicks = 24 Edited September 28, 2023 by erisoov adjust text Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/ Share on other sites More sharing options...
ginerjm Posted September 28, 2023 Share Posted September 28, 2023 Are the 'fields' in each line separated by anything other than a space character? Perhaps a tab? If so, the best solution would be to load the text file into an sql table and then you could do this so much easier with a query. As it is it is a bear. Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/#findComment-1612163 Share on other sites More sharing options...
Solution Barand Posted September 28, 2023 Solution Share Posted September 28, 2023 code $data = file('data.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); usort($data, fn($a, $b)=>substr($a,11,2)<=>substr($b,11,2)); // sort by code number $codes = []; foreach ($data as $d) { $p = strpos($d, ')'); $k = substr($d, 11, $p-10); if (!isset($codes[$k])) { $codes[$k] = 1; } else { $codes[$k]++; } } echo (join("<br>", $data)); echo ("<h3>Total clicks</h3>"); foreach ($codes as $code => $total) { printf("%s Total clicks = %d<br>", $code, $total); } output 15/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Finland Helsinki Uusimaa 18/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Finland Helsinki Uusimaa 21/09/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 Germany Gera Thuringia 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Edmore Michigan 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Alhaurin de la Torre Malaga 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Alhaurin de la Torre Malaga 14/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Madrid Madrid 15/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United States Cedar Springs Michigan 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 Spain Nerja Malaga 16/09/2023 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) 10-9-2023.mp3 United Kingdom Sheffield Sheffield 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Nerja Malaga 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Cartagena Murcia 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Chesaning Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Edmore Michigan 21/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 Spain Nerja Malaga 22/09/2023 05 Day of Atonement 2023 (Día de la Expiación) 17-9-2023.mp3 United States Garden City New York 21/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 Norway Sira Agder 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United Kingdom Sheffield Sheffield 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 Spain Muro del Alcoy Alicante 08/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 09/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Edmore Michigan 10/09/2023 19 The Bread of Life (El Pan de la Vida) 27-8-2023.mp3 United States Cedar Springs Michigan 11/09/2023 20 The Legacy of Jesus (El Legado de Jesús)20-8-2023.mp3 United States Ashburn Virginia 15/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Finland Helsinki Uusimaa 18/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Finland Helsinki Uusimaa 21/09/2023 20 Be With Me Where I Am (Está Conmigo Donde Estoy)20-8-2017.mp3 Germany Gera Thuringia Total clicks 03 Resist the Curse (Resiste la Maldición) Total clicks = 3 04 Feast of Trumpets 2023 (Fiesta de las Trompetas) Total clicks = 19 05 Day of Atonement 2023 (Día de la Expiación) Total clicks = 12 19 The Bread of Life (El Pan de la Vida) Total clicks = 11 20 The Legacy of Jesus (El Legado de Jesús) Total clicks = 1 20 Be With Me Where I Am (Está Conmigo Donde Estoy) Total clicks = 3 1 Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/#findComment-1612164 Share on other sites More sharing options...
erisoov Posted October 11, 2023 Author Share Posted October 11, 2023 (edited) On 9/28/2023 at 1:31 PM, ginerjm said: Are the 'fields' in each line separated by anything other than a space character? Perhaps a tab? If so, the best solution would be to load the text file into an sql table and then you could do this so much easier with a query. As it is it is a bear. I use a space echo nl2br($contents2); I tried your solution but it did not appear to change the totals in any way Edited October 11, 2023 by erisoov amend Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/#findComment-1612339 Share on other sites More sharing options...
ginerjm Posted October 11, 2023 Share Posted October 11, 2023 I believe Barand has already helped you so your response has no meaning any longer. Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/#findComment-1612340 Share on other sites More sharing options...
erisoov Posted October 11, 2023 Author Share Posted October 11, 2023 10 minutes ago, ginerjm said: I believe Barand has already helped you so your response has no meaning any longer. HIs solution did not work Quote Link to comment https://forums.phpfreaks.com/topic/317330-php-count-lines-in-a-text-file-with-a-specific-variable/#findComment-1612342 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.