我正在尝试编写一个php脚本,它将处理m3u文件的每一行并将其写入相应的小时文件。每当过程开始时,我们总是在凌晨00点或午夜12点开始。从第一行到END-OF-HOUR行的所有内容都会进入文件$ month $ day- $ hour.58.15.m3u
$ month和$ day将在整个过程中保持不变并成功完成。在我遇到问题的时候,我遇到了END-OF-HOUR线。假设要发生的是脚本将00小时从00切换到01.前面的0对于0-9小时非常重要。一旦切换发生,它将开始从文件中的下一行写入小时01文件,直到它再次到达END-OF-HOUR行。再次以小时值增加。
这需要在一天中的所有24小时内继续。
发生的事情是该脚本正在将主文件全部复制到小时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
我知道我做错了什么,似乎无法弄清楚它是什么。我们非常感谢您提供的任何帮助。
答案 0 :(得分:0)
我首先要改变它。
$outputfile="$month$day-$hour.58.15";
这需要在while
循环迭代时更新(或至少在您更改小时时)
现在您只是使用您在整个时间内设置的小时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('**foo**', '*'); //outputs 'foo'
OR
并替换每个字符,无论trim('abcFOOcba', 'abc'); //outputs 'FOO'
rtrim(' Foo '); //outputs ' Foo'
修剪右侧,或使用ltrim(' Foo '); //outputs 'Foo '
我不知道为什么他们有3个独立的功能,我更喜欢这个trim($string, $match, $flag);
,其中flag是TRIM_RIGH
,TRIM_LEFT
,TRIM_BOTH
但是,我想你无法得到你想要的一切。 (比如MySql版本)
使用array_map
$a = [ 'Foo ', ' Bar '];
$a = array_map('trim', $a);
print_r($a); //outputs ['Foo', 'Bar']
的文档
MySQL也作为TRIM()
函数SELECT TRIM(BOTH ' ' FROM column) AS foo
它们非常有用。