3

I am trying to write a php script that will process each line of an m3u file and write it to the corresponding hour file. Whenever the process begins we always start at hour 00 or 12am midnight. Everything from the first line until the line that says END-OF-HOUR goes into file $month$day-$hour.58.15.m3u

$month and $day are to stay constant during this entire process and do successfully. Where I run into my problem is when I hit the END-OF-HOUR line. What is suppose to happen is that the script switches $hour from 00 to 01. The preceding 0 is very important for hours 0-9. Once the switch occurs it will start writing from the next line in the file to the hour 01 file until it hits the END-OF-HOUR line again. Once again increasing in hour value.

This needs to continue for all 24 hours of the day.

What is happening is that this script is copying the master file all into the hour 00 file.

Here is what I was able to do on my own:

<?php

//$location="";
$file="PLAYLIST";

$month="Nov";
$day="28";
$hour="00";
$outputlocation="Processed";
$outputfile="$month$day-$hour.58.15";


    //Create Playlist Files Code Here and Working//

$handle = fopen("$file.m3u", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
    // process the line read.



    //Begin Processing
    //If End Of Hour
        if ($line=="END-OF-HOUR"){

            //If Not 11PM
            if ($hour !=="23"){
                $hour="$hour" + 1;
            }

            //If 11PM
            if ($hour =="24"){
                echo "<script>alert('MusicMaster File Processing Complete')</script>";
            }
        }



    //If Not End Of Hour
    if ($line !="END-OF-HOUR"){
        $ofile=file_get_contents("$outputlocation\\$outputfile.m3u");
        $nfile="$ofile
        $line";
        file_put_contents("$outputlocation\\$outputfile.m3u", "$nfile");

    }


}

fclose($handle);
} else {
// error opening the file.

echo "<script>alert('Error Opening MusicMaster File')</script>";
} 

//https://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php
?>

I'm not well versed in looping in php. just very basic if statements and mysql queries.

This is the file it pulls from and outputs to each hour. This is only a snippet:

M:\JINGLES\TOH\LEGAL ID 20170416-A.mp3
M:\ITUNES\Music\Danny Gokey\Rise (Album)\02 If You Ain't In It.mp3
M:\ITUNES\Music\MercyMe\MercyMe, It's Christmas\06 Have a Holly Jolly Christmas.mp3
M:\JINGLES\STANDARD\Stay Tuned.mp3
M:\ITUNES\Music\Royal Tailor\Royal Tailor\06 Ready Set Go.mp3
M:\ITUNES\Music\Third Day\Revelation\03 Call My Name.mp3
M:\THE STORY BEHIND IT\Mandisa - Bleed The Same (Song Story).mp3
M:\PROMOTIONS\Valley Park Flea Market & Resale (6PM 5-29).mp3
M:\PROMOTIONS\FoundationLyrics_com.mp3
M:\PROMOTIONS\VinVlogger_com (5-15-17).mp3
END-OF-HOUR
M:\JINGLES\TOH\LEGAL ID 20170816.mp3
M:\ITUNES\Music\Audio Adrenaline\Kings & Queens\02 Kings & Queens.mp3
M:\ITUNES\Music\Stars Go Dim\Stars Go Dim\01 Doxology.mp3
M:\JINGLES\STANDARD\LIN\LIN-002.mp3
M:\ITUNES\Music\NewSong\Newsong\Christian.mp3
M:\ITUNES\Music\David Dunn\Crystal Clear - EP\02 Have Everything.m4a
M:\THE STORY BEHIND IT\Mandisa - Bleed The Same (Song Story).mp3
M:\PROMOTIONS\Valley Park Flea Market & Resale (6PM 5-29).mp3
END-OF-HOUR

I know I'm doing something wrong and just can't seem to figure out what it is. Any help you can provide would be very much appreciated.

1 Answers1

0

I would start by changing this.

$outputfile="$month$day-$hour.58.15";  

This needs to be updated while the while loop is iterating ( or at least when you change the hour )

Right now you are just using the initial value you set for hour 00 the entire time.

This is why you get the behaviour you have of it not changing the hour, because it's value is never re-assigned as the loop runs.

UPDATE

I took the liberty to rewrite your code. Sorry I'm a perfectionist, the more I looked at the more I didn't like it. ( not tested, as I don't have any files )

$file="PLAYLIST";

//Use an array, it's more concise and readable
$date =[
    'month'      => "Nov",
    'day'        => 28,
    'hour'       => 0,
    'minute'     => 58, //added for extendability
    'second'     => 15 //added for extendability
];

$outputlocation="Processed";
/*** Create Playlist Files Code Here and Working ***/

//open file. We can't proceed without the file, might as well stop here if we can't open it.
if(false === ($handle = fopen("$file.m3u", "r"))) die("Failed to open file.");

//while each line in the file
while (($line = fgets($handle)) !== false) {  
    if(trim(strtoupper($line)) =="END-OF-HOUR"){//If $line = End Of Hour

       //trim removes whitespace from front and back, strtoupper should be self explanitory

       if($hour < 24 ){
            //if less the 12pm (and $line = 'END-OF-HOUR' ) 
            //increment hour and left pad.
             //you may need to use < 23 your logic forgot about it.

            ++$date['hour'];
        }else{
            //else if 12pm (and $line = 'END-OF-HOUR' ) 
            echo "<script>alert('MusicMaster File Processing Complete')</script>";
        }
        continue; 
        /*  
           goes to next line ( iteration of the loop )
           none of the code below this runs.
           logically this is essentially what you had ^
           so there is no need to continue
        */
    }

    // 0 pad left any parts that are len of 1 lenght  
    $fixed = array_map(function($i){
        return (strlen($i) == 1) ? "0$i":$i;
    }, $date);

    /*
      create the filename just before we use it
      not that it matter in PHP, but the original array stays as INT's
      the month is strlen() = 3, so it's unchanged by the above.
     */
    $outputfile = $fixed['month'].$fixed['day'].'-'.$fixed['hour'].'.'.$fixed['minute'].'.'.$fixed['second'];

    //this is all you..
    $ofile=file_get_contents("$outputlocation\\$outputfile.m3u");
    $nfile="$ofile
    $line";
    file_put_contents("$outputlocation\\$outputfile.m3u", "$nfile");

 } //end while

I tested a few things with this:

$date =[
    'month'      => "Nov",
    'day'        => 28,
    'hour'       => 0,
    'minute'     => 58, //added for extendability
    'second'     => 15 //added for extendability
];

$fixed = array_map(function($i){
    return (strlen($i) == 1) ? "0$i":$i;
}, $date);

$outputfile = $fixed['month'].$fixed['day'].'-'.$fixed['hour'].'.'.$fixed['minute'].'.'.$fixed['second'];


print_r($fixed);

echo "\n$outputfile\n";

Outputs

Array
(
    [month] => Nov
    [day] => 28
    [hour] => 00
    [minute] => 58
    [second] => 15
)

Nov28-00.58.15

You can try it in this sandbox

UPDATE

If you wan't to trim all the lines, then just separate this

while (($line = fgets($handle)) !== false) {  
    if(trim(strtoupper($line)) =="END-OF-HOUR"){//If $line = End Of Hour

Like this

while (($line = fgets($handle)) !== false) {  
    $line = trim($line);
    if(strtoupper($line) =="END-OF-HOUR"){//If $line = End Of Hour

A few other things about trim,

  • you can set the character that it trims, by setting the second argument, like this trim('**foo**', '*'); //outputs 'foo'
  • you can set more then one character but it acts like OR and replaces each one regardless the order such as trim('abcFOOcba', 'abc'); //outputs 'FOO'
  • you can trim just the right with rtrim(' Foo '); //outputs ' Foo' or trim just the left with ltrim(' Foo '); //outputs 'Foo '

I don't know why they have 3 separate functions, i'd prefer this trim($string, $match, $flag); where flag is TRIM_RIGH, TRIM_LEFT, TRIM_BOTH but, I guess you can't get everything you want. ( like the MySql version )

You can trim an array for white space pretty easily, by using array_map

 $a = [ 'Foo  ', '  Bar  '];

 $a = array_map('trim', $a);
 print_r($a);  //outputs  ['Foo', 'Bar']

Documentation for PHP Trim

MySQL also as a TRIM() function SELECT TRIM(BOTH ' ' FROM column) AS foo They are very useful.

Documentation for Mysql Trim

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • The hour variable is staying the same still. I've added a few lines to print out what its doing. – Marc Beinder Nov 29 '17 at 04:48
  • Your going to have to check the `if` conditions and see what one is failing, the only way hour doesn't change is if this is fais `$line=="END-OF-HOUR` – ArtisticPhoenix Nov 29 '17 at 05:28
  • @ArtisticPheonix No worries with the rewrite. I appreciate it. I tested it and it worked! Thanks so much! – Marc Beinder Nov 30 '17 at 02:25
  • Cool, glad I could help. – ArtisticPhoenix Nov 30 '17 at 03:54
  • Oh, I'm guessing but I think it may have been `trim` that fixed it for you, all that does is take something like this `"END-OF-HOUR "` and make it like this `"END-OF-HOUR"` it will also remove New Lines, so if you had any extra space in there it wouldn't match, also matching strings is case sensitive, so I figured making sure the case was the same wasn't a bad idea. – ArtisticPhoenix Nov 30 '17 at 03:57
  • By chance could 'trim' remove the 4 spaces at the beginning of each line in the output files? And if so, how? @ArtisticPheonix – Marc Beinder Nov 30 '17 at 04:03