tags:

views:

139

answers:

1

Hi,

Thanks for taking the time out to read.

I have a folder of MP3 files, that I want to use PHP to get the Artist and Title out, as the ID3 tags are non existent.

An example:

01. Wham - Last Christmas.mp3
02. Mariah Carey - All I Want For Christmas Is You.mp3
03. Band Aid - Do They Know It's Christmas Time.mp3

I am sure it's possible, I am just not eloquent enough with regular expressions.

Thanks, Jake.

+2  A: 

Well, for most cases the regex

^\d+\. (.*) - (.*)\.mp3$

should work.

^       start of the string
\d+     at least one digit
\.      a literal dot
(.*)    the artist, in a capturing group. Matches arbitrary characters
        until the following literal is encountered
 -      a literal space, dash, space
(.*)    the title, in a capturing group
\.mp3   the file extension
$       end of the string

You can match your strings with a regular expression with the preg_match function:

$matches = array();
preg_match('/^\d+\. (.*) - (.*)\.mp3$/', "01. Wham - Last Christmas.mp3", $matches);
$artist = $matches[1];
$title = $matches[2];
Joey
I don't suppose you know what function I need to put that in do you?
Jake
Thanks Johannes!
Jake
+1 for the extremely good explanation.
Pekka
Yes... telling the user to use explode is not good enough...
AntonioCS
Antonio: That wasn't the question and it's not a particularly superior alternative as you need either multiple explodes or substring magic—both of which aren't too pretty. Also it's not the worst way of learning to use regular expressions.
Joey