开发者

How do I use regex to get Artist and Title from an MP3 filename?

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.


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜