php - 如何使用正则表达式从 MP3 文件名中获取艺术家和标题?

标签 php regex

感谢您抽出时间阅读。

我有一个 MP3 文件文件夹,我想使用 PHP 来获取艺术家和标题,因为 ID3 标签不存在。

一个例子:

01。 Wham - Last Christmas.mp3
02。玛丽亚凯莉 - All I Want For Christmas Is You.mp3
03。创可贴 - 他们知道现在是圣诞节吗.mp3

我相信这是可能的,我只是对正则表达式不够 Eloquent 。

谢谢, jack 。

最佳答案

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

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

关于php - 如何使用正则表达式从 MP3 文件名中获取艺术家和标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1843172/

相关文章:

php - Auth0 "The JWT string must contain two dots"

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - 用php锁定mysql表

python - 通过正则表达式修复 .bib 文件标题

java - 包括所有特殊字符的正则表达式模式

php - composer 无法安装 slim api 框架

PHP preg_replace 非字母数字导致数据库插入失败

regex - 找到一个作为模式给出的行词另一个行词,awk,perl,sed,grep?

regex - 正则表达式词法分析

php - PHP 中的惰性函数定义 - 这可能吗?