Answer the question
In order to leave comments, you need to log in
How to cut the file name and split into variables through regular expressions?
Hello. At me not absolutely standard task on regexes. I'm a noob in regular seasons, so I'm asking for help. There are file names:
02. Goodwin [Heroes of Comic Books] - Shouting Sirens (freestyle)
12. Stylish Billy feat. aka Ahmed - Chisto e (Stylish Billy prod.)
04.Nuttkase feat. Bird - If Friend (Khasol-Version) (Phunk Masta Seven prod.)
02.Dr.MokRolog,Old Primus,JustSmile,Vine Bi - Elektrograch (Pro100 Beatz prod.)
- If the name starts with "01." (numbers and dot, cut them out)
- We take the content up to the space and up to the hyphen "-", this is saved in the artist field
- We take the content after the hyphen and space "-", store it in the name field
- After the name, in most cases there are either brackets "( )" or square brackets "[ ]", and sometimes the brackets are repeated. Is it possible in this variant to add the first closing brackets to the file name, and to take the last ones and insert them into the beatmaker field?!
$artist = 'Стильный Билли';
$feat = 'aka Ахмед';
$name = 'Чисто ё';
$beatmaker = 'Стильный Билли';
$artist = 'Nuttkase';
$feat = 'Птаха';
$name = 'Если Друг (Хасол-Версия)';
$beatmaker = 'Phunk Masta Seven';
$artist = 'Dr.MokRolog';
$feat = 'Old Primus,JustSmile,Vine Bi';
$name = 'Электрограч';
$beatmaker = 'Pro100 Beatz';
Answer the question
In order to leave comments, you need to log in
function parseTrack($str) {
[$first_part, $second_part] = explode(' - ', $str);
$first_part = preg_replace('~^\d+\. ?~', '', $first_part);
$artists = preg_split('~ feat\. |,~', $first_part);
$artist = $artists[0];
$feat = array_slice($artists, 1);
preg_match('~^(.*?)(?: \(([^(]+) prod\.?\))?$~', $second_part, $match);
return [
'artist' => $artist,
'feat' => $feat,
'name' => $match[1],
'beatmaker' => isset($match[2]) ? $match[2] : null
];
}
/^([\d]+\.)?\s?([^-]+)\s-\s(.+)\((.+)?\)$/img
seems to be so
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question