Nasıl bir MP3 dosya gelen sanatçı ve başlık almak için regex kullanabilirim?

1 Cevap php

Okumak için zaman ayırdığınız için teşekkür ederiz.

Ben ID3 etiketleri olmayan olarak, Sanatçı ve Başlık çıkmak için PHP kullanmak istediğiniz, MP3 dosyaları bir klasör var.

Örnek:

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

Bunu ben sadece düzenli ifadelerle yeterince anlamlı değilim, mümkün eminim.

Thanks, Jake.

1 Cevap

Peki, çoğu için regex

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

çalışması gerekir.

^       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

Sen preg_match fonksiyonu ile normal bir ifade ile dizeleri eşleşebilir:

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