...

View Full Version : Extracting data from flat file



samuurai
11-17-2007, 01:53 PM
Hi,

I've written a script which extracts data from FreeDB files and it's been fairly straightforward until this last bit.

The files are laid out with

DTITLE=blahblahblah
YEAR=2007

etc etc..

But this last big has me stumped...This is the layout:

# Track frame offsets
# 150
# 18500
# 32005
# 48620
#
# Something else Something Else

So far, i've got my code to search for "Track frame offsets", but how do I get it to iterate down until it's reached the "# " line?

I tried doing an initial search with a variable which contained "Track frame offsets", but then had that variable changed to the next line (ie, 150), but the top of my script kept re-initialising the variable to "Track frame offsets".

P.S. Yes, I know they're comments, FreeDB file format is a joke! :)

kbluhm
11-17-2007, 03:37 PM
// pre-set data
$record = FALSE;
$track = array();

// open
if ( $fp = fopen( './file.ext', 'r' ) )
{
// iterate
while ( $line = fgets( $fp ) )
{
$line = trim( $line );
// start?
if ( $line == '# Track frame offsets' )
{
$record = TRUE;
}
// end?
elseif ( $line == '#' )
{
$record = FALSE;
}
// are we recording?
if ( $record )
{
$track[] = $line;
}
}
// close
fclose( $fp );
}

// display
print_r( $track );

Oh yeah, if you do not wish to track the first line (# Track frame offsets), put the // are we recording? if-statement ahead of the // start? if-statement.

samuurai
11-17-2007, 05:00 PM
[php]


if ( $fp = fopen( './file.ext', 'r' ) )

while ( $line = fgets( $fp ) )


Hi kbluhm.. thanks for the code! I'm new to coding and your idea of using a recording mechanism is very cool...

I'm using the function "file" to open each file. It loads each line into an array.

Should I use your code with fopen and fgets as well is this way?

Here is my code thus far and further below is a full example of one of the files i'm reading:


<?php

$dir = "/var/www/html/blues/";

$dtitle = 'DTITLE=';
$dgenre = 'DGENRE=';
$dyear = 'DYEAR=';
$ttitle = 'TTITLE';


$tfostring = "Track frame offsets:";

$record = FALSE;
$duration = array();

function trimAndCase($input) {
$input = trim($input);
$input = ucwords(strtolower($input));
return $input;

}

//create table to display data
echo '<table border="1">';
echo '<tr>';
echo '<td>File</td><td>Artist</td><td>Release</td><td>Track Number</td><td>Track Name</td><td>Track Artist</td><td>Duration<td>Year</td><td>Genre</td><td>Test</td>';
echo '</tr>';
echo '<tr>';
$dir = opendir($dir);
while (($file = readdir($dir)) !== false) {
if(!is_dir($file)) {
$lines = file("/var/www/html/blues/".$file); //the readdir function changes the variable to "Resource Handle 3, so I had to specify the path manually again.
foreach($lines as $line) {
trim($line);
//<PROBLEMATIC CODE>
//get track length by subtracting the previous track offset from the next, then divide it by 75
if(strstr($line, $tfostring)) {
$record = TRUE;
}
elseif($line == "#")
{
$record = FALSE;
}
if ( $record )
{
$duration[] = $line;
}
//</PROBLEMATIC CODE>
if(strstr($line, $dtitle)) {
$line = substr($line, strlen($dtitle));
$elements = explode("/", $line);
if(count($elements) > 2) {
$artist = trimAndCase($elements[0]);
$release = trimAndCase($elements[1] . "/" . $elements[2]);
}
elseif(count($elements) == 1) {
$artist = trimAndCase($elements[0]);
$release = trimAndCase($elements[0]);
}
else {
$artist = trimAndCase($elements[0]);
$release = trimAndCase($elements[1]);
}
if($artist == "V/A") {
$artist = "Various";
}
}
// Find the Genre
if(strstr($line, $dgenre)) {
$line = substr($line, strlen($dgenre));
$genre = trim($line);
}
// Find the Year
if(strstr($line, $dyear)) {
$line = substr($line, strlen($dyear));
$year = trim($line);
}
//Find the Song and Artist name of each song
if(strstr($line, $ttitle)) {
$line = substr($line, strlen($ttitle));
$elements = explode("=", $line);
$tracknumber = trim($elements[0] +1);
if(strstr($line, "/")) {
$elements = explode("/", $elements[1]);
$trackartist = trimAndCase($elements[0]);
$trackname = trimAndCase($elements[1]);
} else {
$trackartist = $artist;
$trackname = trimAndCase($elements[1]);
}


echo "<tr>";
echo '<td><a href="' . "/blues/" . $file . '">' . $file . '</a></td>';
echo "<td>" . $artist . "</td>";
echo "<td>" . $release . "</td>";
echo "<td>" . $tracknumber . "</td>";
echo "<td>" . $trackname . "</td>";
echo "<td>" . $trackartist . "</td>";
echo "<td>" . $duration . "</td>";
echo "<td>" . $year . "</td>";
echo "<td>" . $genre . "</td>";
//echo "<td>" . $linecount . "</td>";
echo "</tr>";
}
}
}
}

echo "</table>";

closedir($dir);
?>


freedb.txt

# xmcd
#
# Track frame offsets:
# 9150
# 41236
# 68060
# 74462
# 82675
# 98052
# 104101
# 119020
#
# Disc length: 1869 seconds
#
# Revision: 1
# Processed by: cddbd v1.5.1PL0 Copyright (c) Steve Scherf et al.
# Submitted via: audiograbber 1.83.01
#
DISCID=7206d308
DTITLE=We Acediasts / We Acediasts / Pre Acediasts
DYEAR=
DGENRE=Japanese post-DFA PsycheSpazz
TTITLE0=We Acediasts / Kousho
TTITLE1=We Acediasts / Ibasho
TTITLE2=Pre Acediasts / Omoshiroi Omoshiroi Omoshiroi
TTITLE3=Pre Acediasts / Anata Ga Suki Yo
TTITLE4=Pre Acediasts / Hitori Heiwa Na Nyapo
TTITLE5=Pre Acediasts / Kajiroudou
TTITLE6=Pre Acediasts / Un No Ii Yatsu
TTITLE7=Pre Acediasts / Kyoukai Ongaku
EXTD=
EXTT0=
EXTT1=
EXTT2=
EXTT3=
EXTT4=
EXTT5=
EXTT6=
EXTT7=
PLAYORDER=



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum