爆炸文件名以分隔名称和扩展名在PHP中不起作用?

Phi*_*l_t 2 php filenames explode period scandir

我正在构建一个类,它返回一个字符串,该字符串将自动包含文件夹中的文件,有点像HTML文件的加载器.

这是将被调用的方法:

function build_external_file_include($_dir){
    $_files = scandir($_dir);
    $_stack_of_file_includes = "";//will store the includes for the specified file.
foreach ($_files as $ext){
    //split the file name into two;
    $fileName = explode('.',$ext, 1);//this is where the issue is.

    switch ($fileName[1]){
        case "js":
            //if file is javascript
             $_stack_of_file_includes =  $_stack_of_file_includes."<script type='text/javascript' src='".$dir.'/'.   $ext ."'></script>";

            break;
        case "css";//if file is css
             $_stack_of_file_includes =  $_stack_of_file_includes."<link rel=\"stylesheet\" type=\"text/css\" href=\"".$dir.'/'. $ext."\" />";
            break;
        default://if file type is unkown
             $_stack_of_file_includes =  $_stack_of_file_includes."<!-- File: ".  $ext." was not included-->";
    }


}
return $_stack_of_file_includes;
}
Run Code Online (Sandbox Code Playgroud)

所以,这运行没有任何错误.但是,它没有做它应该做的事情......或者至少我打算做什么.从技术上讲,

$fileName[1] 应该是扩展名 js

$fileName[0] 应该是文件的名称 main

$fileName[0]main.js.

确实爆炸不承认.

先感谢您.

Sam*_*son 5

您强制生成的数组包含1个元素,这会导致它具有整个文件名.

explode( '.', $ext, 1 )
Run Code Online (Sandbox Code Playgroud)

应该是

explode( '.', $ext );
Run Code Online (Sandbox Code Playgroud)

证明:http://codepad.org/01DLpo6H