duanpenpan5796 2010-04-28 14:15
浏览 16
已采纳

PHP不完整代码 - 扫描目录,仅在名称以x开头或结尾时包含

I posted a question before but I am yet limited to mix the code without getting errors.. I'm rather new to php :(

( the dirs are named in series like this "id_1_1" , "id_1_2", "id_1_3" and "id_2_1" , "id_2_2", "id_2_3" etc.)

I have this code, that will scan a directory for all the files and then include a same known named file for each of the existing folders.. the problem is I want to modify a bit the code to only include certain directories which their names:

  1. ends with "_1"
  2. starts with "id_1_"

I want to create a page that will load only the dirs that ends with "_1" and another file that will load only dirs that starts with "id_1_"..

<?php 

include_once "$root/content/common/header.php";
include_once "$root/content/common/header_bc.php";
include_once "$root/content/" . $page_file . "/content.php";

$page_path = ("$root/content/" . $page_file);

$includes = array();

$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($page_path), 
            RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $file) {
    if($file->isDir()) {
        $includes[] = strtoupper($file . '/template.php');
    }
}

$includes = array_reverse($includes);

foreach($includes as $file){
   include $file;
}

include_once "$root/content/common/footer.php";

?>

Many Thanks!

  • 写回答

1条回答 默认 最新

  • doupao2277 2010-04-28 18:22
    关注
    foreach($iterator as $file) {
        if($file->isDir()) {
            // getFilename() actually gives the directory name, when it's um, a directory.
            $dirName = $file->->getFilename();
            if (substr($dirName, 0, 5) === 'id_1_') {
                $includes[] = strtoupper($file . '/template.php');
            }
        }
    }
    

    There's other ways to do this, but I tried to only add simple functions and logic, in hopes you will understand it.

    Ends with would look like

    if (substr($dirName, -2) === '_1')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化