假设服务器中有一个名为" mp3"的文件夹。有大量的mp3文件," a1.mp3"," a2.mp3"," a3.mp3"," a4.mp3", " ao0.mp3"," ao2.mp3"," ao3.mp3"," ci1.mp3"," ci2。 mp3和#34;等
foreach (glob('sound/*.mp3') as $filename) {
if (!in_array($filename,$exclude_files)) {
$outputtext .= "<p>".preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))."</p>";
}
}echo $outputtext;
我的源代码如上所示,当前结果为:
A1
A2
A3
AO0
AO2
AO3
CI1
CI2
但我想要的结果是将所有相同的模式分组如下:
a - 123
ao - 023
ci - 12
感谢您的回复。
答案 0 :(得分:0)
foreach (glob($path.'*.mp3') as $filename) {
if (!in_array($filename,$exclude_files)) {
if (preg_match('/[0-9]/', $filename, $matches, PREG_OFFSET_CAPTURE)) {
// Starting after "sound/", so the starting position is 6
$filenamestr[$n] = substr($filename, 6, $matches[0][1]-6+1);
$n++;
}
}
}
$totalfiles = $n--;
$mp3 = "";
$o = 0;
$p = 0;
for ($l = 0; $l < $totalfiles; $l++) {
if (substr($filenamestr[$l], 0, strlen($filenamestr[$l])-1) == substr($filenamestr[$l-1], 0, strlen($filenamestr[$l-1])-1)) {
// Connect file names to an array when current file name is the same as previous one
$p++;
$mp3file[$o][$p] = substr($filenamestr[$l], -1);
$mergemp3file[$o] .= $mp3file[$o][$p];
} else {
// Produce another array for file name when current file name is different from previous one
$o++;
$p = 0;
$mp3file[$o][$p] = substr($filenamestr[$l], 0, strlen($filenamestr[$l])-1)." - ".substr($filenamestr[$l], -1);
// Collect sound files for each file name
$mergemp3file[$o] .= $mp3file[$o][$p];
}
}
// Generate merged sound files for each file name
$totalmp3 = $o--;
for ($o = 1; $o < $totalmp3; $o++) {
$outputtext .= "<br/>".$mergemp3file[$o].chr(13);
}