【问题标题】:Using glob to filename parameters使用 glob 文件名参数
【发布时间】:2011-07-01 17:24:25
【问题描述】:

这是我需要的,我有文件:“page-Home1.php”、“page-Contact2.php”。 是的,我知道他们没有漂亮的名字,但这不是我现在担心的,我需要的是 glob 按 1、2、3 等顺序回显文件。

我目前有:

<?php
foreach (glob("page-*") as $filename) {
    $result = str_replace("page-","", $filename);
    $result = str_replace(".php","", $result);
    echo "<li><a href='" . $filename ."'/>". $result . "</a></li><tr>";
}
?>

虽然这只会以随机顺序吐出它们,但我需要它来编号顺序....有什么想法吗?

【问题讨论】:

    标签: php glob


    【解决方案1】:
    $files = glob(dirname(__FILE__).'/page-*.php');
    
    foreach ($files as $file) {
        $result[preg_replace('#[^0-9]#','', $file)]['file'] = $file;
        $result[preg_replace('#[^0-9]#','', $file)]['name'] = str_replace(array("page-", ".php"), array('', ''), $file);;
    }
    
    sort($result);
    
    foreach($result as $data) {
        echo $data['file'].' -> '.$data['name'].'<br>';
    }
    

    【讨论】:

    • 只获取文件名?
    【解决方案2】:

    在迭代之前对数组进行排序。

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多