Suppose there is a folder in the server called "mp3" with plenty of mp3 files, "a1.mp3", "a2.mp3", "a3.mp3", "a4.mp3", "ao0.mp3", "ao2.mp3", "ao3.mp3", "ci1.mp3", "ci2.mp3" etc.
foreach (glob('sound/*.mp3') as $filename) {
if (!in_array($filename,$exclude_files)) {
$outputtext .= "<p>".preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))."</p>";
}
}echo $outputtext;
My source code is as above and the current result is:
a1
a2
a3
ao0
ao2
ao3
ci1
ci2
But the result what I want is to group all same patterns as follows:
a - 123
ao - 023
ci - 12
Thanks for all your reply.
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);
}