【发布时间】:2012-05-12 14:26:00
【问题描述】:
我正在构建一个返回字符串的类,该字符串将自动包含文件夹中的文件,有点像 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;
}
所以,这运行没有任何错误。但是,它没有做它应该做的事情……或者至少我打算做的事情。从技术上讲,
$fileName[1] 应该是扩展名js
$fileName[0]应该是文件名main
但是
$fileName[0] 是main.js。
爆炸不识别.?
提前谢谢你。
【问题讨论】:
-
将
$fileName = explode('.',$ext, 1);更改为$fileName = explode('.',$ext); -
我建议你使用
pathinfo()而不是explode -
@felipe.. 你会用这些文件名做什么
jquery-.1.7.1.min.js -
diEcho,您好,感谢您的关注!这个特定的类为 CSS 和 JS 文件构建了一组 HTML 外部文件引用。正如您在此处看到的,脚本设置为遍历指定目录中的文件并返回一个字符串,该字符串具有引用外部文件的 HTML 标记,在本例中为该文件夹中的文件。
标签: php filenames explode period scandir