简体   繁体   中英

PHP - Regex to remove the string between parenthesis

I'm trying to split a filename into 3 parts here.

Example: Artist - Title ( Mix ) or Artist - Title [ Mix ]

My code so far.

preg_match('/^(.*) - (.*)\.mp3$/', $mp3, $matches);
$artist = $matches[1];
$title = $matches[2];
echo "File: $mp3" . "Artist: $artist" . "\n" . "Title: $title" . "<br />";

This is getting me the Artist and the Title. The problem I have is that Mix is either between () or [ ]. I'm not sure how to modify my regex in order to capture that part.

This isn't a 100% regex solution, but I think it's the most elegant you'll get.

Basically, you want to capture (anything) or [anything] , which can be represented as \\(.*\\)|\\[.*\\] . Then, make that a capture group, and double escape it, to get (\\\\(.*\\\\)|\\\\[.*\\\\]) .

Unfortunately, this captures the () or [] as well, so you have to strip those; I simply used substr($matches[3], 1, -1) to do the job:

$mp3 = "Jimmy Cross - I Want My Baby Back (Remix).mp3";
preg_match('/^(.*) - (.*) (\\(.*\\)|\\[.*\\])\.mp3$/', $mp3, $matches);
$artist = $matches[1];
$title = $matches[2];
$mix = substr($matches[3], 1, -1);
echo "File: $mp3" . "<br/>" . "Artist: $artist" . "<br/>" . "Title: $title" . "<br />" . "Mix: $mix" . "<br />";

Prints out:

File: Jimmy Cross - I Want My Baby Back (Remix).mp3
Artist: Jimmy Cross
Title: I Want My Baby Back
Mix: Remix

Try '/^(.*) - ([^\\(\\[]*) [\\(\\[] ([^\\)\\]]*) [\\)\\]]\\.mp3$/'

However, this may not be the most efficient way to do it.

I would use named subpatterns for this specific case.

$mp3s = array(
    "Billy May & His Orchestra - T'Ain't What You Do.mp3",
    "Shirley Bassey - Love Story [Away Team Mix].mp3",
    "Björk - Isobel (Portishead remix).mp3",
    "Queen - Another One Bites the Dust (remix).mp3"
);

$pat = '/^(?P<Artist>.+?) - (?P<Title>.*?)( *[\[\(](?P<Mix>.*?)[\]\)])?\.mp3$/';

foreach ($mp3s as $mp3) {
    preg_match($pat,$mp3,$res);
    foreach ($res as $k => $v) {
        if (is_numeric($k)) unset($res[$k]);
        // this is for sanitizing the array for the output
    }
    if (!isset($res['Mix'])) $res['Mix'] = NULL;
    // this is for the missing Mix'es
    print_r($res);
}

will output

Array (
    [Artist] => Billy May & His Orchestra
    [Title] => T'Ain't What You Do
    [Mix] => 
)
Array (
    [Artist] => Shirley Bassey
    [Title] => Love Story
    [Mix] => Away Team Mix
)
Array (
    [Artist] => Björk
    [Title] => Isobel
    [Mix] => Portishead remix
)
Array (
    [Artist] => Queen
    [Title] => Another One Bites the Dust
    [Mix] => remix
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM