Jump to content

[SOLVED] Can't fathom the regex needed for this


awpti

Recommended Posts

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.

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!

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.