pkSML Posted December 24, 2006 Share Posted December 24, 2006 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].mp3I 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. Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/ Share on other sites More sharing options...
c4onastick Posted December 24, 2006 Share Posted December 24, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-147131 Share on other sites More sharing options...
Nicklas Posted December 24, 2006 Share Posted December 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-147274 Share on other sites More sharing options...
pkSML Posted December 24, 2006 Author Share Posted December 24, 2006 Thanks, guys. Your help is much appreciated.I got the regular expression worked out.[code]preg_match('/^(.*)\s?-\s?([^\(]+)\s?\(.*\.mp3$/', $filename, $match);[/code]It worked on about 575 files successfully! Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-147402 Share on other sites More sharing options...
c4onastick Posted December 24, 2006 Share Posted December 24, 2006 Excellent! Glad to help. Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-147409 Share on other sites More sharing options...
spookztar Posted October 13, 2007 Share Posted October 13, 2007 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, Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-368601 Share on other sites More sharing options...
Rithiur Posted October 13, 2007 Share Posted October 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-368661 Share on other sites More sharing options...
spookztar Posted October 14, 2007 Share Posted October 14, 2007 Thanx for responding, Rithiur.Two things:1) Using "^" and "$" isn't really required when using preg_match(), is it?2) To better accomodate any future formats, wouldn't it be more advisable to just specifically exclude certain dangerous extensions? Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-369245 Share on other sites More sharing options...
c4onastick Posted November 13, 2007 Share Posted November 13, 2007 [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. Quote Link to comment https://forums.phpfreaks.com/topic/31739-solved-preg_match-help/#findComment-390368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.