Jump to content

[SOLVED] preg_match help


pkSML

Recommended Posts

I have many song files and am trying to generate a playlist.

Example filename:
Foundation Brass - Savior, Like A Shepherd Lead Us (92462-99144) play[bf].mp3

I would like to capture the artist and song name with a regular expression. Everything up to the dash is the artist and everything up to the ( is the title.

Here is what I have:
[code]$filename = "Foundation Brass - Savior, Like A Shepherd Lead Us (92462-99144) play[bf].mp3";
$pattern = '/([\w ]+[^ -])([\w -,]+[^ \(])/';
preg_match($pattern, $filename, $results);[/code]

Thanks for any help.
Link to comment
Share on other sites

You've got the right idea. Here's what I would do:
[code]preg_match('/^([\w ]+) - ([^\(]+) \(.*\.mp3$/', $filename, $match);[/code]
This should work if all your files look like the one example one you posted.
Alternatively, not knowing much about the files you're going after (and with some of my own experience with doing stuff like this) this might be safer:
[code]preg_match('/^([\w ]+)\s?-\s?([^\(]+)\s?\(.*\.mp3$/', $filename, $match);[/code]
This allows for optional whitespace between the dash and the artist or the song title. Since this regex is fairly general, I added '\.mp3$' to require that matches only apply to mp3 files, which might save you some hassle if there's other things floating around with the mp3's.
Link to comment
Share on other sites

I would use [url=http://www.php.net/preg_split]preg_split()[/url] to do the job.

ex

[code=php:0]list($artist, $title) = preg_split('/ - | \(/', $info);[/code]



Then I just need to do something like this to loop thru my mp3 files and get the artist + title at the same time:
[hr]
[code=php:0]foreach(glob("some_dir/*.mp3") as $info) {

list($artist, $title) = preg_split('/ - | \(/', $info);
// Do what you want here...

}[/code][hr]
Link to comment
Share on other sites

  • 9 months later...
Hi guys,

I have a related issue but with all types of music/video media files. Here's what I've done so far:

preg_match('/[.]{1,80}(\.[[:alpha:]]{1,5})/', $filename)

Here's what I'm looking for:

"1-80 of any type of character on the left side of the dot, and 1-5 alpha characters on the right side of the dot". What is the generally right/responsible thing to do/best practices when regex'ing filenames? I'm thinking disallowing certain extensions for the sake of security etc.

Rock on,
Link to comment
Share on other sites

spookztar: It would probably be better idea to post a new topic than reply in one marked as "solved".

[quote author=spookztar link=topic=119794.msg715989#msg715989 date=1192289869]
"1-80 of any type of character on the left side of the dot, and 1-5 alpha characters on the right side of the dot"
[/quote]

Here's a regex according to your spesification:

[code]preg_match('/^.{1,80}(\.[a-z]{1,5})$/', $filename)[/code]

As for security measures, if you have some sort of file upload system, then personally I would only allow spesific file types that are needed for the particular purpose. Other than that, it's kinda  hard to say anything spesific, since it depends a lot on what you do with them.
Link to comment
Share on other sites

  • 5 weeks later...
[quote author=spookztar link=topic=119794.msg716635#msg716635 date=1192368723]
1) Using "^" and "$"  isn't really required when using preg_match(), is it?
[/quote]
Wrapping your regex in "^" and "$" ensures that the match is applied to the entire file name. It anchors the regex to beginning of the string ("^") and the end of the string ("$") and pretty much says, "the beginning to the end must match this".
[quote author=spookztar link=topic=119794.msg716635#msg716635 date=1192368723]
2) To better accomodate any future formats, wouldn't it be more advisable to just specifically exclude certain dangerous extensions?
[/quote]
You can do that, but most of the time it's better to create an alternation of what you explicitly want to allow. Sometimes new dangerous extensions can creep in unexpectedly if you just exclude a few.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.