need exploding array help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JackM

    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($arr ayData){
    $elements=explo de(" ",$arrayDat a);
    $returnString = "";
    for
    ($i=0;$i<count( $elements)-4;$i++){$return String.=$elemen ts[$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_st ring( $album );
    $ID = "";

    $ret = array();

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

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

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

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

    $track = mysql_escape_st ring( trim($line) );
    if (strlen($track) > 0)
    $ret[] = @sprintf("INSER T INTO `lists` VALUES(\"%s\", \"%s\",
    \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",$I D, $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($inse rts))
    {
    foreach($insert s as $idx => $sql)
    echo $sql . "\n";
    } else echo "error\n";

    //writes results to new textfile
    $filename = 'e:\mirc\CDList s\database.txt' ;
    $fp = fopen($filename , "a");
    if (is_array($inse rts))
    {
    foreach($insert s as $idx => $sql)
    fwrite($fp,stri pslashes("$sql\ n"));
    }
    fclose($fp);
    ?>
  • chotiwallah

    #2
    Re: need exploding array help


    JackM wrote:[color=blue]
    > 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[/color]
    and[color=blue]
    > translates each entry into the form where they can be inserted into[/color]
    my[color=blue]
    > mySQL database in the proper fields although it is currently being
    > written to another text file because of the problem I have below. The[/color]
    [color=blue]
    > 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.[/color]
    The[color=blue]
    > 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[/color]
    when[color=blue]
    > 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[/color]
    I[color=blue]
    > modify the script to ignore any dashes (should there be any) after[/color]
    the[color=blue]
    > third one? Once I can get this bug worked out I can change from[/color]
    writing[color=blue]
    > to another textfile to inserting directly in the database. Thanks for[/color]
    [color=blue]
    > 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($arr ayData){
    > $elements=explo de(" ",$arrayDat a);
    > $returnString = "";
    > for
    > ($i=0;$i<count( $elements)-4;$i++){$return String.=$elemen ts[$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_st ring( $album );
    > $ID = "";
    >
    > $ret = array();
    >
    > while ($line = fgets($fd, 4096))
    > {
    > $arrayData = explode("-", trim($line));
    >
    > $test1 = @$arrayData[3];
    > $test1 = stripLast2($tes t1); //strips last two items from the[/color]
    arrayData[3][color=blue]
    >
    > $songData = explode(" ", @$arrayData[3]);
    >
    > $ar=array_rever se($songData); //reverses array so I can handle last[/color]
    two[color=blue]
    > items in right place
    >
    > $track = mysql_escape_st ring( trim($line) );
    > if (strlen($track) > 0)
    > $ret[] = @sprintf("INSER T INTO `lists` VALUES(\"%s\",[/color]
    \"%s\",[color=blue]
    > \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", \"%s\");",$I D, $album,
    > $arrayData[0], @$arrayData[1], @$arrayData[2], $test1, @$ar[2],[/color]
    @$ar[0]);[color=blue]
    > }
    > 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[/color]
    extension[color=blue]
    > to be entered into the database text file...</b><br><br></p>";
    >
    > ?>
    > <div align="right">
    > <form name="form" action="<? print $_SERVER['PHP_SELF'] ?>"[/color]
    method="post">[color=blue]
    > <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($inse rts))
    > {
    > foreach($insert s as $idx => $sql)
    > echo $sql . "\n";
    > } else echo "error\n";
    >
    > //writes results to new textfile
    > $filename = 'e:\mirc\CDList s\database.txt' ;
    > $fp = fopen($filename , "a");
    > if (is_array($inse rts))
    > {
    > foreach($insert s as $idx => $sql)
    > fwrite($fp,stri pslashes("$sql\ n"));
    > }
    > fclose($fp);
    > ?>[/color]


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

    micha

    Comment

    • JackM

      #3
      Re: need exploding array help

      chotiwallah wrote:
      [color=blue]
      >
      > maybe use " - " (note the spaces) instead of "-" as separator for
      > explode?
      >
      > micha
      >[/color]

      Duh! Just what I needed.

      Thanks micha. Sometimes I can't see the forest for the trees.

      Comment

      Working...