Ortix Posted December 29, 2010 Share Posted December 29, 2010 so I'm working on this system which requires me to inject anime show names into a DB for an index. I get them in this form: [Kira-Fansub]_HIGHSCHOOL_OF_THE_DEAD_-_04_(BD_1920x1080_x264_AAC) [F0E73009].mkv I need to get it in this form: Highschool Of The Dead sometimes the words are space seperated. the main part is to extract the text in between the ] and ( and with that i mean [stuff]extract this(other stuff) and sometimes it looks like this: [stuff]extract[stuff] I have NO CLUE how to do this preg_match_all doesn't get me any further.. maybe in stages? Quote Link to comment https://forums.phpfreaks.com/topic/222858-extracting-data-from-string/ Share on other sites More sharing options...
johnny86 Posted December 29, 2010 Share Posted December 29, 2010 preg_match('#(?:\)|\])([^\(\[]+)(?:\(|\[)#i', '[Kira-Fansub]_HIGHSCHOOL_OF_THE_DEAD_-_04_[bD_1920x1080_x264_AAC)', $match); maybe this could work for you. $match[1] holds the string _HIGHSCHOOL_OF_THE_DEAD_-_04_ Quote Link to comment https://forums.phpfreaks.com/topic/222858-extracting-data-from-string/#findComment-1152382 Share on other sites More sharing options...
Ortix Posted December 29, 2010 Author Share Posted December 29, 2010 pretty effing darn close! I'll use it! (this is prolly the hardest to decipher since most of them are just space separated) Quote Link to comment https://forums.phpfreaks.com/topic/222858-extracting-data-from-string/#findComment-1152386 Share on other sites More sharing options...
laffin Posted December 29, 2010 Share Posted December 29, 2010 <?php $name='[Kira-Fansub]_HIGHSCHOOL_OF_THE_DEAD_-_04_(BD_1920x1080_x264_AAC) [F0E73009].mkv'; $str=preg_replace(array('/(\s*?[\[(].*?[\])]\s*?)/','/(-.*)/','/(_)/','/( )/'),array('','',' ',' '),$name); echo $str; ?> output HIGHSCHOOL OF THE DEAD How it works: 1st match ('/(\s*?[\[(].*?[\])]\s*?)/') catches [] and () items and removes them ('') 2nd match ('/(-.*)/') drops everything after the dash, including the dash ('') 3rd match (/(_)/) replaces underscores with spaces (' ') 4th match (/( )/) Double spaces replaced with single space (' '): Quote Link to comment https://forums.phpfreaks.com/topic/222858-extracting-data-from-string/#findComment-1152413 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.