473,396 Members | 1,966 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,396 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 1980

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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.