awpti Posted February 5, 2008 Share Posted February 5, 2008 Here's what I need from the following; Size: 8,007,272 Band: Band Name Song: Song Name Extension: ext 12/18/2007 05:43 PM 8,007,272 Band Name - Song Name.ext 12/18/2007 05:43 PM 4,007,272 Band Name - Song Name.ext Obviously, some will have single word names for band/song, and some will have more. I just need to capture the bandname, songname, filesize and file extension. Thoughts? Or.. anyone just have a plain ol' regex for it handy? It goes without saying, I don't understand anything beyond the bare basics of regex functionality. Link to comment https://forums.phpfreaks.com/topic/89489-solved-cant-fathom-the-regex-needed-for-this/ Share on other sites More sharing options...
PHP Monkeh Posted February 5, 2008 Share Posted February 5, 2008 I'm not great at regex either (and I don't pretend to be) so I often seek solutions other ways. Seeing asthough the length of the time & date and the spaces between are always the same length, you could return the pieces of info you need by applying a substr() to it. $neededInfo = substr($longString, 30, strlen($longString)); Replace $longString with whatever the line is that's containing the data (I hope you're reading the file line-by-line). Then if you're wanting to split up all the other info, I guess you can use explode(), and some strpos() to figure out where the info begins/ends. If you'd like to the code on how to do this I could probably get it working once I'm home, but at work right now sorry! Link to comment https://forums.phpfreaks.com/topic/89489-solved-cant-fathom-the-regex-needed-for-this/#findComment-458632 Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 preg_match_all("@^(\d{2}/\d{2}/\d{4}\s{2}\d{2}:\d{2} [AP]M)\s+([0-9,]*)\s+(.*)\s+-\s+(.*)\.(.*)$@",$string,$matches) Link to comment https://forums.phpfreaks.com/topic/89489-solved-cant-fathom-the-regex-needed-for-this/#findComment-458672 Share on other sites More sharing options...
awpti Posted February 5, 2008 Author Share Posted February 5, 2008 Got it resolved with this; <?php $input = <<<EOF 12/18/2007 05:43 PM 8,007,272 Band Name - Song Name.ext 12/18/2007 05:43 PM 4,007,272 Band Name - Song Name.ext -rw------- 1 root utmp 11657088 2008-02-04 20:48 Band Name - Song Name.ext EOF; $fields = array('matched_line','file_size','band_name','song_name','extension'); $results = array(); preg_match_all('/^.+(?:utmp|[AP]M)\s*([\d,]+)\s*(?:\d+-.+?:\d+\s*)?([\w ]+)\s+-\s+([\w ]+)\.(\w+)$/m', $input, $matches, PREG_SET_ORDER); foreach ($matches as $row) { $row = array_combine($fields,$row); $row['file_size'] = str_replace(',', '', $row['file_size']); $results[] = $row; } var_dump($results); Link to comment https://forums.phpfreaks.com/topic/89489-solved-cant-fathom-the-regex-needed-for-this/#findComment-458937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.