我在目录中有很多电影,文件名包含尺寸。
我希望从#1中取出800,600。来自#2的720,来自#3的1080,等等。
寻找一些帮助来调整我到目前为止所获得的内容:
$re = '/^(.*?)\-(\d{3,4})p+|(\d{2,4})x(\d{2,4})+\.mov|mp4$/';
preg_match($re, $filename ,$matches);
#1匹配:(获取一些我不需要的额外内容......?)
Array
(
[0] => 800x600.mov
[1] => {empty_string}
[2] => {empty_string}
[3] => 800
[4] => 600
)
#2匹配:
Array
(
[0] => testing-720p
[1] => testing
[2] => 720
)
我显然有些不对劲,任何意见都会非常感激!
答案 0 :(得分:1)
你不必做一些复杂的事情。如果您的文件结构始终具有“ - ”和扩展名,请将它们拆分。例如
$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);
}
输出
$ php test.php
Array
(
[0] => 800
[1] => 600
)
Array
(
[0] => 720p
)
Array
(
[0] => 1080p
)
Array
(
[0] => 640
[1] => 480
)