简体   繁体   English

列出带有前缀的特定文件

[英]listing specific files with prefix

Hi I would like to list all specific files with the prefix myfile-.. 嗨,我想列出所有带有前缀myfile- ..的特定文件。

This is my current code... and no files are listed 这是我当前的代码...并且未列出文件

   <select name="s1">
        <option value="" selected="selected">-----</option>
        <?php 
            foreach(glob(dirname(__FILE__) . '/*') as $filename){
                $filename = basename($filename);
               // if (preg_match('/\.(php)$/i', $filename)) {
                    if (preg_grep('~^myfile-.*\.php$~', $filename)) {
                    echo "<option value='" . $filename . "'>".$filename."</option>";
                }
            }
        ?>

if I use this... all files are listed 如果我使用此...列出所有文件

 if (preg_match('/\.(php)$/i', $filename)) {

while no files are listed if using this 如果使用此选项,则没有列出文件

if (preg_grep('~^myfile-.*\.php$~', $filename)) {

Why make it so hard? 为什么要这么难?

If(substr($filename, 0, 7) == "myfile-"){
    echo "<option value='" . $filename . "'>".$filename."</option>";
}

Substr looks at the first seven characters and looks if it's "myfile-" Substr查看前七个字符,并查看它是否为“ myfile-”

Shortest way: 最短的方法:

foreach (glob(dirname(__FILE__)."myfile-*.php") as $filename) {
        echo "<option value='" . $filename . "'>".$filename."</option>";
    }

i figured it out..anyway..thank you all 我想通了..无论如何..谢谢大家

<select name="s1">
      <option value="" selected="selected">-----</option>
        <?php 
           foreach(glob(dirname(__FILE__) . '/myfile-*.{php}', GLOB_BRACE) as $filename){
       $filename = basename($filename);
       echo "<option value='" . $filename . "'>".$filename."</option>";
    }
        ?>
    </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM