微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

感谢您抽出宝贵时间阅读.

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

一个例子:

01. Wham – Last Christmas.mp3
02. Mariah Carey – 我想要的圣诞节是You.mp3
03.乐队援助 – 他们知道这是圣诞节Time.mp3

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

谢谢,
杰克.

解决方法:

好吧,对于大多数情况下的正则表达式

^\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];

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐