Frantumn
Frantumn

Reputation: 1764

PHP's trim doing more than I want

I'm trying to get a list of mp3 files within a directory (/music/category/). Then I trim the directory, and then trim the mp3 extention.

The problem I'm getting with some files is that if the file name begins with the letters a,u,d,i,o (audio) as found in the string of line 3, it trims them.

So a song named "awesome" ends up displaying as "wesome".

Anyone know why this is?

$files = glob('audio/' . $category . '/*.{mp3}', GLOB_BRACE);   
foreach($files as $file) {   
$title = trim($file, '/audio/' . $category . "/");  
$withoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $title);  
echo $withoutExt;
}

Upvotes: 0

Views: 67

Answers (1)

Juris Malinens
Juris Malinens

Reputation: 1271

I would do it this way:

$it = new FilesystemIterator('audio/' . $category, FilesystemIterator::SKIP_DOTS);
foreach ($it as $fileinfo) {
    echo $fileinfo->getBasename('.mp3'). "\n";
}

or:

$files = glob('audio/' . $category . '/*.{mp3}', GLOB_BRACE);
foreach($files as $file) {
    echo pathinfo($file, PATHINFO_FILENAME);
}

Upvotes: 1

Related Questions