我使用此php代码检索存储在目录中的文件。
if ($handle = opendir('FolderPath')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n <br />" ;
}
}
closedir($handle);
}
该目录仅包含PHP
文件,我如何能够从回显的结果中删除扩展名?示例:(index.php
将变为index
)
要做到这一点,最简单的方法是使用的glob
函数:
foreach (glob('path/to/files/*.php') as $fileName) {
//extension .php is guaranteed here
echo substr($fileName, 0, -4), PHP_EOL;
}
的优势,glob
这里是你可以破除那些讨厌readdir
和opendir
电话。唯一的“缺点”是的值也$fileName
将包含路径。但是,这很容易解决(只需添加一行):
foreach (glob('path/to/files/*.php') as $fullName) {
$fileName = explode('/', $fullName);
echo substr(
end($fileName),//the last value in the array is the file name
0, -4),
PHP_EOL;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句