Kajcioch
Kajcioch

Reputation: 175

Trouble reading from txt file

This is a specific problem I got and can't understand what I did wrong. Don't flag this question again because of "possible duplicate" because I KNOW how to open and print out a file. This is not what I'm asking for.

This is my txt file:

onceagain|Tristam - Once Again.mp3|gh|Tristam|Once Again;
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS;
blue|Au5 Fractal - Blue.mp3|drumstep|Au5 & Fractal|Blue;
airplanes|B.O.B - Airplanes (Feint Remix).mp3|dnb|B.O.B.|Airplanes (Feint Remix);
sea|Camellia - Flying in the Flow of Deep-Sea.mp3|dubstep|Camellia|Flying in the Flow of Deep-Sea;
onslaught|CENOB1TE - Onslaught.mp3|dubstep|CENOB1TE|Onslaught;
snakeeyes|Feint - Snake Eyes (feat. CoMa).mp3|drumstep|Feint|Snake Eyes (feat. CoMa);
wontbealone|Feint - We Won't Be Alone (feat. Laura Brehm).mp3|dnb|Feint|We Won't Be Alone (feat. Laura Brehm);
vine|Tristam - The Vine.mp3|drumstep|Tristam|The Vine;
frameofmind|Tristam & Braken - Frame of Mind.mp3|dnb|Tristam & Braken|Frame of Mind;
heist|Noisestorm - Heist.mp3|trap|Noisestorm|Heist;

I could print out everthing, but I want to print them out using for loop, because I will be puting everything in seperate variables later:

$myfile = fopen("songs.txt", "r");
$songs = fread($myfile,filesize("songs.txt"));
$lines = count(file("songs.txt")) - 1;

for ($x = 0;  $x <= $lines; $x++) {
    echo strtok($songs, ";");
    $songs = strstr($songs, ';');
}

Unfortunately, this is what output i recieve:

onceagain|Tristam - Once Again.mp3|gh|Tristam|Once Again
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS

I need this to be fixed.

Upvotes: 1

Views: 104

Answers (1)

James Paterson
James Paterson

Reputation: 2890

You could always use explode() to break up your file. Explode separates by a delimiter, and then puts the data into an array. You can then iterate over this array and print out the data.

<?php
$myfile = fopen("songs.txt", "r");
$songs = fread($myfile,filesize("songs.txt"));
$lines = count(file("songs.txt")) - 1;

$songs = explode(";",$songs);

for ($x = 0;  $x <= $lines; $x++) {
    echo $songs[$x]."<br>";
}

Using original solution:

Your problem comes because you do not remove the semicolon at the end. When strstr() is used, the needle is left in the result. On your pass, you split correctly, get the song and print it, and then when you use strstr() you remove the first song, leaving you with:

;
seksikeks|AContrari - SEKS I KEKS.mp3|dubstep|AContrari|SEKS I KEKS;
blue|Au5 Fractal - Blue.mp3|drumstep|Au5 & Fractal|Blue;
...

Then on the next pass you get the next song, seksikeks, and print it, however when you use strstr() it finds the delimiter as the first character and so does not remove anything. This is why it continues to print because the line is not being removed, and the variable $songs stays as it is shown above.

The code below fixes the issue: By removing first the song, and then the semicolon, you can iterate properly.

$myfile = fopen("songs.txt", "r");
$songs = fread($myfile,filesize("songs.txt"));
$lines = count(file("songs.txt")) - 1;

for ($x = 0;  $x <= $lines; $x++) {
    $song = strtok($songs, ";");
    echo $song;
    $songs = strstr($songs, $song);
    $songs = strstr($songs, ';');
}

Here is also the code for what you have asked for in the comments:

<?php
$myfile = fopen("songs.txt", "r");
$songs = fread($myfile,filesize("songs.txt"));
$lines = count(file("songs.txt")) - 1;

$songs = explode(";",$songs);

$result = array();

for ($x = 0;  $x <= $lines; $x++) {
    $song = explode('|',$songs[$x]);
    $songHandle = $song[0]; // First item is array item name
    $songArray = array(); // Create an array and assign properties to it
    $songArray['file'] = $song[1]; 
    $songArray['genre'] = $song[2];
    $songArray['artist'] = $song[3];
    $songArray['title'] = $song[4];
    $result[$songHandle] = $songArray; // Add our array to the the result
}

var_dump($result); // Show our result

Upvotes: 1

Related Questions