Php combine identical lines in text file

Joined
Oct 11, 2023
Messages
3
Reaction score
1
I have the following code
Code:
$fp = fopen('/output/report_sort.txt', 'w');
if(!$fp)
die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
die('Could not write to text file.');

// Print the totals for each category
$data = file('/output/report_sort.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 ("<font color='blue'><b><h3>Total clicks</h3></b></font>");
foreach ($codes as $code => $total)  {
printf("%s <font color='red'><b>Total clicks = %d<br></b></font>", $code, $total);
}
echo "<br />";

which from this data

Code:
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 France 
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 23 The Wedding Part 1 (La Boda) 30-7-2023.mp3 Ireland Dublin Leinster

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

outputs the totals for each category as follows

Code:
03 Resist the Curse (Resiste la Maldición) Total clicks = 2
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 = 32
06 Feast of Tabernacles 2023 (Fiesta de los Tabernáculos) Total clicks = 4
07 The Eighth Day 2023 (El Octavo Día) Total clicks = 17
19 The Bread of Life (El Pan de la Vida) Total clicks = 20
20 Be With Me Where I Am (Está Conmigo Donde Estoy) Total clicks = 4
20 The Legacy of Jesus (El Legado de Jesús) Total clicks = 2
23 The Wedding Part 1 (La Boda) Total clicks = 1

what I would like is to combine lines which are duplicated to show 1 line as 1 click as follows

Code:
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 France 
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster

To be shown in the totals as
03 Resist the Curse (Resiste la Maldición) Total clicks = 1
07 The Eighth Day 2023 (El Octavo Día) Total clicks = 1

the same for all the other categories
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Consider using php's built-in array functions in this way

report_sort.txt
Code:
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
06/10/2023 03 Resist the Curse (Resiste la Maldición) 11-12-2022.mp3 France
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster
09/10/2023 23 The Wedding Part 1 (La Boda) 30-7-2023.mp3 Ireland Dublin Leinster

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
PHP:
<?php
    define('REPORT_FILE_NAME', 'report_sort.txt');
    if (! file_exists('/output/' . REPORT_FILE_NAME))
        die('There was a problem accessing the file: ' . REPORT_FILE_NAME);

    $data = file('/output/' . REPORT_FILE_NAME, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
    if (! is_array($data))
        die('File: ' . REPORT_FILE_NAME . ' contains no data!');

    if (count($data) < 2)
        die('File: ' . REPORT_FILE_NAME . ' contain only one line with data!');

    $unique_index = [];
    $filtered_lines = [];
    foreach ($data as $line) {
        if (in_array(getIndex($line), $unique_index))
            continue;
        else {
            $unique_index[] = getIndex($line);
            $filtered_lines[] = cleanLine($line);
        }
    }

    $total = 1; // demo purpose

    function cleanLine($line) {
        $line_temp_arr = explode('.mp3', $line);
        $line_temp_arr = explode(' ', $line_temp_arr[0]);
        return join(' ', array_slice($line_temp_arr, 1, -1));
    }

    function getIndex($line) {
        return explode(' ', $line)[1];
    }
?>
<style>
    html,
    body {
        padding: 0;
        margin: 1rem;
    }
    h3 {
        font-weight: bold;
        color: blue;
    }
    ul {
        list-style-type: none;
        padding: 0;
    }
    li span.total-clicks {
        font-weight: bold;
        font-size: 95%;
        color: red;
        margin-left: .5rem;
    }
</style>
<!-- <font color='blue'><b><h3>Total clicks</h3></b></font> -->
<h3>Total clicks</h3>
<ul>
<?php
    foreach ($filtered_lines as $line) {
        echo "<li>{$line} <span class=\"total-clicks\">Total clicks = {$total}</span></li>";
    }
?>
</ul>

filtered lines.png
 
Joined
Oct 11, 2023
Messages
3
Reaction score
1
I think what I am wanting to do is a sort per country and city i.e for data

The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3

the totals would be
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Cartagena Murcia Total clicks = 1
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Nerja Malaga Total clicks = 1
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Cedar Springs Michigan Total clicks = 1
05/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Spain Madrid Madrid Total clicks = 1
06/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 United States Edmore Michigan Total clicks = 4
08/10/2023 07 The Eighth Day 2023 (El Octavo Día) 1-10-2023.mp3 Ireland Dublin Leinster Total clicks = 7

Then the same for each category
 
Joined
Oct 11, 2023
Messages
3
Reaction score
1
Hi are u a programmer ? I have a dating website deal from a firm ,we could be making over $25,000 monthly okay on a dating website deal can we work together ? Mail me
Sorry I already have a date with destiny how about you?
John 3:3, Romans 10:9-10
 
Joined
Nov 14, 2023
Messages
1
Reaction score
0
To combine identical lines in a text file using PHP, you can follow these steps:
Online PHP compiler playground CodeExampler
1. Read the File:
Use file_get_contents() or file() to read the content of the text file into an array or string.

Code:
php
   $fileContent = file_get_contents('yourfile.txt');
   // or
   $fileLines = file('yourfile.txt', FILE_IGNORE_NEW_LINES);

2. Remove Duplicate Lines:
Use array_unique() to remove duplicate lines from the array.

Code:
php
   $uniqueLines = array_unique($fileLines);

3. Combine Lines:
If needed, use implode() to join the lines into a single string.

Code:
php
   $combinedContent = implode("\n", $uniqueLines);

4. Write Back to File:
Use file_put_contents() or another method to save the modified content back to the file.

Code:
php
   file_put_contents('yourfile.txt', $combinedContent);

Here's a concise example combining these steps:

PHP:
$fileLines = file('yourfile.txt', FILE_IGNORE_NEW_LINES);
$uniqueLines = array_unique($fileLines);
$combinedContent = implode("\n", $uniqueLines);
file_put_contents('yourfile.txt', $combinedContent);

This PHP script reads a text file, removes duplicate lines, combines the unique lines, and then writes the result back to the same file. Adjust the file paths and names as needed for your specific case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top