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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...