views:

147

answers:

4

Hi,

I'd like to get parts of the filename

filenameblabla_2009-001_name_surname-name_surname

I'd like to get: 2009-001, name_surname, name_surname

I've come up with this, but it's no good

preg_match("/(?:[0-9-]{8})_(?:[a-z_]+)-(?:[a-z_]+)/", $filename, $matches);

Any pointers?

Thanks! BR

A: 
preg_match("([0-9-]{8})_([^_]+)_([^_]+)-([^_]+)_([^_]+)$", $filename, $matches);
Igor Korkhov
Code doesn't work, missing pattern delimiters
kemp
A: 

Assuming filename format doesn't change:

preg_match('#(\d{4}-\d{3})_(.*?)-(.*?)$#', $filename, $match);

Updated version to handle extension:

preg_match('#(\d{4}-\d{3})_(.*?)-(.*?)\.(.*?)$#', $filename, $match);
kemp
I forgot about the extension. I've tried playing with: ([a-z_]*?)$# but it won't work.
Mission
Update answer to account for extensions too
kemp
A: 

if your file name is always that structure

$name = "blabla_2009-001_name_surname-name_surname";
$s = explode("_",$name,3);
$t = explode("-",end($s));
print "$s[1] $t[0] $t[1]\n";

output

$ php test.php
2009-001 name_surname name_surname
ghostdog74
This gives `2009-001`, `name`, `surname-name`
kemp
That would actually give me: blabla, 2009-001, name, surname-name surname...
Mission
The whole explode yes, but he's printing only the 2nd, 3rd and 4th elements of the returned array.
kemp
@mission, see updated.
ghostdog74
A: 

Thanks you all,

I've updated my own solution As "nikc" pointed out...i just needed some direction...

here's the code if anyone will need a similar solution...

Thank you all!

preg_match('/([0-9-]{8})_([^-]+)-([a-z_]+).([a-z]+)/', $filename, $matches);
Mission