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.

Link to comment
Share on other sites

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
Share on other sites

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