By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

need exploding array help

P: n/a
Let me attempt to explain my problem. I have a crude php script that
takes a text list of songs that was generated by an mp3 list program and
translates each entry into the form where they can be inserted into my
mySQL database in the proper fields although it is currently being
written to another text file because of the problem I have below. The
lines from the mp3 text file will look like this:

Al Green - The Supreme Al Green - 01 - Tired Of Being Alone.mp3
5.04MB 244k
Al Green - The Supreme Al Green - 02 - I Can't Get Next To You.mp3
6.52MB 242k
Al Green - The Supreme Al Green - 03 - Let's Stay Together.mp3
5.74MB 242k

The script reads it into an array and keys on the "-" to explode it. The
script works just fine on those, doing what I want it to and putting
everything into it's proper field. But my problem comes into play when
there is more than the three dashes in any selection like:

Al Green - The Supreme Al Green - 12 - Sha-la-la (Make Me Happy).mp3
6.05MB 283k

That totally wrecks havoc with what I need the script to do. How can I
modify the script to ignore any dashes (should there be any) after the
third one? Once I can get this bug worked out I can change from writing
to another textfile to inserting directly in the database. Thanks for
any help.

The PHP scripting I'm using is:
<?php
// Get the info
$q = @$_POST['q'] ;
$trimmed = trim($q); //trim whitespace from the stored variable
$dir = "e:/mirc/CDLists/";
$file = ($dir . $q);

//used to strip the last two items from array (size and bitrate)
function stripLast2($arrayData){
$elements=explode(" ",$arrayData);
$returnString = "";
for
($i=0;$i<count($elements)-4;$i++){$returnString.=$elements[$i]." "; }
return $returnString;
}
//reads song list identified in $trimmed into array
function read_songlist($file)
{
if (!file_exists($file))
{
echo "Doesn't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading $file\n";
return false;
}

$path = ($file);
$album = basename($path, ".txt");

$album = mysql_escape_string( $album );
$ID = "";

$ret = array();

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));

$test1 = @$arrayData[3];
$test1 = stripLast2($test1); //strips last two items from the arrayData[3]

$songData = explode(" ", @$arrayData[3]);

$ar=array_reverse($songData); //reverses array so I can handle last two
items in right place

$track = mysql_escape_string( trim($line) );
if (strlen($track) > 0)
$ret[] = @sprintf("INSERT INTO `lists` VALUES(\"%s\", \"%s\",
\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",$ID, $album,
$arrayData[0], @$arrayData[1], @$arrayData[2], $test1, @$ar[2], @$ar[0]);
}
fclose($fd);
return $ret;
}

//the html part to display the text box to enter the filename
echo "<p align='center'><br><b>Please enter the filename and extension
to be entered into the database text file...</b><br><br></p>";

?>
<div align="right">
<form name="form" action="<? print $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Filename" /><span
style="font-family:Verdana,Arial,Helvetica,sans-serif;"> ex:
Albums010.txt</span>
</form>
</div>
<?php

//writes results to html page
$inserts = read_songlist($file);
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
echo $sql . "\n";
} else echo "error\n";

//writes results to new textfile
$filename = 'e:\mirc\CDLists\database.txt';
$fp = fopen($filename, "a");
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
fwrite($fp,stripslashes("$sql\n"));
}
fclose($fp);
?>
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

JackM wrote:
Let me attempt to explain my problem. I have a crude php script that
takes a text list of songs that was generated by an mp3 list program and translates each entry into the form where they can be inserted into my mySQL database in the proper fields although it is currently being
written to another text file because of the problem I have below. The lines from the mp3 text file will look like this:

Al Green - The Supreme Al Green - 01 - Tired Of Being Alone.mp3
5.04MB 244k
Al Green - The Supreme Al Green - 02 - I Can't Get Next To You.mp3
6.52MB 242k
Al Green - The Supreme Al Green - 03 - Let's Stay Together.mp3
5.74MB 242k

The script reads it into an array and keys on the "-" to explode it. The script works just fine on those, doing what I want it to and putting
everything into it's proper field. But my problem comes into play when there is more than the three dashes in any selection like:

Al Green - The Supreme Al Green - 12 - Sha-la-la (Make Me Happy).mp3
6.05MB 283k

That totally wrecks havoc with what I need the script to do. How can I modify the script to ignore any dashes (should there be any) after the third one? Once I can get this bug worked out I can change from writing to another textfile to inserting directly in the database. Thanks for any help.

The PHP scripting I'm using is:
<?php
// Get the info
$q = @$_POST['q'] ;
$trimmed = trim($q); //trim whitespace from the stored variable
$dir = "e:/mirc/CDLists/";
$file = ($dir . $q);

//used to strip the last two items from array (size and bitrate)
function stripLast2($arrayData){
$elements=explode(" ",$arrayData);
$returnString = "";
for
($i=0;$i<count($elements)-4;$i++){$returnString.=$elements[$i]." "; }
return $returnString;
}
//reads song list identified in $trimmed into array
function read_songlist($file)
{
if (!file_exists($file))
{
echo "Doesn't exist\n";
return false;
}

$fd = @fopen($file, 'r');
if (!is_resource($fd))
{
echo "Error reading $file\n";
return false;
}

$path = ($file);
$album = basename($path, ".txt");

$album = mysql_escape_string( $album );
$ID = "";

$ret = array();

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));

$test1 = @$arrayData[3];
$test1 = stripLast2($test1); //strips last two items from the arrayData[3]
$songData = explode(" ", @$arrayData[3]);

$ar=array_reverse($songData); //reverses array so I can handle last two items in right place

$track = mysql_escape_string( trim($line) );
if (strlen($track) > 0)
$ret[] = @sprintf("INSERT INTO `lists` VALUES(\"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",$ID, $album,
$arrayData[0], @$arrayData[1], @$arrayData[2], $test1, @$ar[2], @$ar[0]); }
fclose($fd);
return $ret;
}

//the html part to display the text box to enter the filename
echo "<p align='center'><br><b>Please enter the filename and extension to be entered into the database text file...</b><br><br></p>";

?>
<div align="right">
<form name="form" action="<? print $_SERVER['PHP_SELF'] ?>" method="post"> <input type="text" name="q" />
<input type="submit" name="Submit" value="Filename" /><span
style="font-family:Verdana,Arial,Helvetica,sans-serif;"> ex:
Albums010.txt</span>
</form>
</div>
<?php

//writes results to html page
$inserts = read_songlist($file);
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
echo $sql . "\n";
} else echo "error\n";

//writes results to new textfile
$filename = 'e:\mirc\CDLists\database.txt';
$fp = fopen($filename, "a");
if (is_array($inserts))
{
foreach($inserts as $idx => $sql)
fwrite($fp,stripslashes("$sql\n"));
}
fclose($fp);
?>

maybe use " - " (note the spaces) instead of "-" as separator for
explode?

micha

Jul 17 '05 #2

P: n/a
chotiwallah wrote:

maybe use " - " (note the spaces) instead of "-" as separator for
explode?

micha


Duh! Just what I needed.

Thanks micha. Sometimes I can't see the forest for the trees.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.