473,218 Members | 1,432 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

need exploding array help

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
2 1972

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Andy Cunningham | last post by:
Hi all, Has anyone seen a problem like this before: http://80.177.2.28/phpinfo.php This is a webserver on my home PC, and when viewed from there it looks fine. However, from computers across...
9
by: mark | last post by:
I have an array as follows $sounds = array(); $sounds = "fishes/1/sound.php"; $sounds = "fishes/2/sound.php"; $sounds = "fishes/3/sound.php"; $sounds = "fishes/4/sound.php"; $sounds =...
9
by: Nathan Rose | last post by:
Here's my problem. I am reading from a text file using this: if (!file_exists($file)) { echo "Don't exist\n"; return false; } $fd = @fopen($file, 'r'); if (!is_resource($fd))
4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
9
by: Jesphachia Twhongle | last post by:
A year ago, it would have been a tragedy to lose Munich to string pulling by Microsoft Insiders who want tacquiredt Linux. Obviously, the forces of corruption, evil and lying have raised their...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: SpreadTooThin | last post by:
If you are deriving a new class from another class, that you must (I assume) know the initializer of the other class. So in myClass import array class myClass(arrary.array): def...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.