我正在尝试编写一个php脚本,它将处理m3u文件的每一行,并将其写入相应的小时文件。每当这个过程开始时,我们总是在午夜00点或12点开始。从第一行到表示END-OF-HOUR的行进入文件$month$day-$hour.58.15.m3u
$month和$day将在整个过程中保持不变,并成功完成。我遇到问题的地方是当我到达下班时间线时。假设发生的是脚本将$hour从00切换到01。前面的0对于小时0-9非常重要。一旦发生切换,它将开始从文件中的下一行写入hour 01文件,直到再次命中小时结束行。小时值再次增加。
这需要持续一天中的所有24小时。
发生的情况是,此脚本将主文件复制到hour 00文件中。
这是我自己能够做到的:
<?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
?>我不太精通php中的循环。只是非常基本的if语句和mysql查询。
这是它从每个小时中提取并输出到的文件。这只是一个代码片段:
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我知道我做错了什么,只是不知道是什么。如果您能提供任何帮助,我们将不胜感激。
发布于 2017-11-29 12:27:26
我会从改变这一点开始。
$outputfile="$month$day-$hour.58.15"; 这需要在while循环迭代时更新(或者至少在更改小时时更新)。
现在,您只需在整个时间内使用为hour 00设置的初始值。
这就是为什么你可以获得它的行为而不改变时间,因为它的值在循环运行时永远不会被重新赋值。
更新
我擅自重写了你的代码。对不起,我是一个完美主义者,我越看越不喜欢。(未测试,因为我没有任何文件)
$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我用这个测试了一些东西:
$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";输出
Array
(
[month] => Nov
[day] => 28
[hour] => 00
[minute] => 58
[second] => 15
)
Nov28-00.58.15您可以在此sandbox中尝试
更新
如果你不想修剪所有的行,那么就把这行分开
while (($line = fgets($handle)) !== false) {
if(trim(strtoupper($line)) =="END-OF-HOUR"){//If $line = End Of Hour像这样
while (($line = fgets($handle)) !== false) {
$line = trim($line);
if(strtoupper($line) =="END-OF-HOUR"){//If $line = End Of Hour关于trim的其他一些事情,
通过设置第二个参数,trim('**foo**', '*'); //outputs 'foo'
trim('abcFOOcba', 'abc'); //outputs 'FOO'
OR,无论顺序如何,都会替换每个字符。例如,ltrim(' Foo '); //outputs 'Foo '
可以用rtrim(' Foo '); //outputs ' Foo'修剪右侧,也可以用
我不知道为什么他们有三个独立的函数,我更喜欢这个标志是TRIM_RIGH,TRIM_LEFT,TRIM_BOTH的trim($string, $match, $flag);,但是,我猜你不能得到你想要的一切。(类似于MySql版本)
通过使用array_map,可以非常容易地将数组裁剪为空白
$a = [ 'Foo ', ' Bar '];
$a = array_map('trim', $a);
print_r($a); //outputs ['Foo', 'Bar']PHP Trim文档
MySQL也可以作为TRIM()函数的SELECT TRIM(BOTH ' ' FROM column) AS foo,它们非常有用。
https://stackoverflow.com/questions/47544891
复制相似问题