如何使用正则表达式从MP3文件名中获取Artist和Title?

时间:2009-12-03 21:50:28

标签: php regex

感谢您抽出时间阅读。

我有一个MP3文件夹,我想用PHP来获取Artist和Title,因为ID3标签不存在。

一个例子:

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

我确信这是可能的,我对正则表达式不够雄辩。

谢谢, 杰克。

1 个答案:

答案 0 :(得分:7)

嗯,大多数情况下正则表达式

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

应该有用。

^       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

您可以使用preg_match函数将字符串与正则表达式匹配:

$matches = array();
preg_match('/^\d+\. (.*) - (.*)\.mp3$/', "01. Wham - Last Christmas.mp3", $matches);
$artist = $matches[1];
$title = $matches[2];