我有一个非常好用的PHP脚本,但我希望回显一个列出月份的标题。文件名中包含_01,_02,_03等。我已经创建了一个数月的数组,但我想弄清楚最好的方法是做什么。
如果文件名包含_01,则回显1月。否则,如果文件名包含_02,则回显2月。有人知道这种情况的最佳做法吗?
foreach (glob("*.mov") as $filename)
$theData = file_get_contents($filename) or die("Unable to retrieve file data");
$months = ['_01', '_02', '_03', '_04', '_05', '_06', '_07', '_08', '_09', '_10', '_11', '_12'];
答案 0 :(得分:1)
可能类似于此 - 基于月份号码仅在文件名中一次
的事实foreach($theData as $filename){
preg_match('/_(\d{2})/', $filename, $match);
echo date('F',strtotime($match[1].'/20/2000'));
}
答案 1 :(得分:1)
试试这个:
foreach (glob("*.mov") as $filename)
$theData = file_get_contents($filename) or die("Unable to retrieve file data");
$months = ['January' => '_01', 'February' => '_02', 'March' => '_03', 'April' => '_04', 'May' => '_05', 'June' => '_06', 'July' => '_07', 'August' => '_08', 'September' => '_09', 'October' => '_10', 'November' => '_11', 'December' => '_12'];
foreach($months as $key => $month){
if(strpos($filename,$month)!==false){
echo $key;
}
}
答案 2 :(得分:0)
这是我能够提出的
$files = [
"file_01",
"file_02asdf",
"file_03_sfsa",
"file_04_23",
"file_05 cat"
];
foreach($files as $file){
preg_match("/_(\d{2,2})/", $file, $matches);
echo date("F", strtotime("2000-{$matches[1]}-01"))."\n";
}
导致:
一月
二月
三月
四月
可能
答案 3 :(得分:0)
也许这是另一种方式,但preg_match似乎更聪明
function getMonthnameByFilename($filename,$months) {
foreach($months as $index => $m) {
if(strpos($filename,$m) !== false) {
return date("F",strtotime("2013-".($index+1)."-1"));
}
}
return false;
}