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.
确实爆炸不承认.?
先感谢您.
您强制生成的数组包含1个元素,这会导致它具有整个文件名.
explode( '.', $ext, 1 )
Run Code Online (Sandbox Code Playgroud)
应该是
explode( '.', $ext );
Run Code Online (Sandbox Code Playgroud)
证明:http://codepad.org/01DLpo6H
| 归档时间: |
|
| 查看次数: |
4233 次 |
| 最近记录: |