5 php sorting preg-replace readdir
我正在做一个小画廊.我想从目录中读取文件名,并在删除一些前导数字和文件扩展名后打印下面的文件名.
我有两个版本的代码.
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) { // does the dissected array have more than one part
$extension = end($parts); // set to we can see last file extension
$bfile= substr($file, 2); //strips the first two characters
$cfile= preg_replace(('/\d/'),' ',$bfile);//remove numbers
$cfile= preg_replace(('/_/'),' ',$cfile);
$cfile= preg_replace(('/.jpg/'),' ',$cfile);
if ($extension == "jpg" OR $extension == "JPG") // is extension ext or EXT ?
echo "<td><img src=\"weddings2/$file\"><br />$cfile</td>\n";
}
}
closedir($dir); // Close the directory after we are done
Run Code Online (Sandbox Code Playgroud)
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);
foreach ($files as $file)
if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
$table_cell .= "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
echo $table_cell;
Run Code Online (Sandbox Code Playgroud)
是的,我知道我很蠢.Arghhh!
编辑:您的代码缺少大括号
你有
foreach (...)
code
code
它应该是
foreach (...) {
code
code
}
只需将代码放在$ parts和foreach循环之后的最后一个$ cfile之间,只需在循环中添加大括号,这样就可以放入更多代码.另请注意,如果两个代码段中的条件不同,则必须确定哪个一个使用或是否将它们组合成一个条件.
$current_dir = "$DOCUMENT_ROOT"."/weddings2/";
$dir = opendir($current_dir); // Open the sucker
$files = array();
while ($files[] = readdir($dir));
sort($files);
closedir($dir);
foreach ($files as $file) {
//MANIPULATE FILENAME HERE, YOU HAVE $file...
if ($file <> "." && $file <> ".." && !preg_match("/^hide/i",$file))
echo "<td><img src='".'weddings2/'. rawurlencode($file) ."'><br />$cfile</td>\n";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32045 次 |
| 最近记录: |