从txt文件读取时出现问题


Trouble reading from txt file

这是我遇到的一个特定问题,无法理解我做错了什么。不要因为"可能重复"而再次标记此问题,因为我知道如何打开和打印文件。这不是我想要的。

这是我的txt文件:

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;

我可以打印出所有东西,但我想使用for循环打印出来,因为我稍后将把所有东西都放在单独的变量中:

$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, ';');
}

不幸的是,这就是我收到的输出:

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

我需要解决这个问题。

您可以始终使用explode()来分解文件。分解以分隔符分隔,然后将数据放入数组中。然后,您可以对这个数组进行迭代并打印出数据。

<?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>";
}

使用原始解决方案:

你的问题是因为你没有删除末尾的分号。当使用strstr()时,针头留在结果中。在你的通行证上,你正确地分开,拿到歌曲并打印出来,然后当你使用strstr()时,你会删除第一首歌曲,留下:

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

然后在下一次传递中,您获得下一首歌曲seksikks并打印它,但是当您使用strstr()时,它会将分隔符作为第一个字符,因此不会删除任何内容。这就是为什么它继续打印的原因,因为行没有被删除,变量$songs保持如上所示。

下面的代码修复了这个问题:通过先删除歌曲,然后删除分号,可以正确地迭代。

$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, ';');
}

这也是你在评论中要求的代码:

<?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