views:

40

answers:

1

I have a lot of movies in directories with filenames containing their dimensions.

  1. descriptor-800x600.mov
  2. cool_animation-720p.mp4
  3. reactor-1080p.mov
  4. test-640x480.mov

I'm looking to pull out the 800, 600 from #1. 720 from #2, 1080 from #3, etc.

Looking for some help to tweak what I've got so far:

$re = '/^(.*?)\-(\d{3,4})p+|(\d{2,4})x(\d{2,4})+\.mov|mp4$/';
preg_match($re, $filename ,$matches);

Matches for #1: (Getting some extra stuff I don't need...?)

Array
(
    [0] => 800x600.mov
    [1] => {empty_string}
    [2] => {empty_string}
    [3] => 800
    [4] => 600
)

Matches for #2:

Array
(
    [0] => testing-720p
    [1] => testing
    [2] => 720
)

I've obviously got something wrong, any input would be greatly appreciated!

+1  A: 

you don't have to do it some complicated. If your file structure is always having "-" and extension, split on them. eg

$a = array("descriptor-800x600.mov", "cool_animation-720p.mp4", "reactor-1080p.mov","test-640x480.mov");
foreach ($a as $name){
    $s = preg_split("/[-.]/",$name);
    $what_i_want=$s[1];
    $w = explode("x",$what_i_want);
    print_r($w);
}

output

$ php test.php
Array
(
    [0] => 800
    [1] => 600
)
Array
(
    [0] => 720p
)
Array
(
    [0] => 1080p
)
Array
(
    [0] => 640
    [1] => 480
)
ghostdog74
Appreciate the help ghostdog. Narrowing it down into smaller bits was the way to go.
wdavis