![](/img/trans.png)
[英]PHP Gallery with lightbox, folder import and descriptions from file names
[英]how to exclude file extensions when using file names as captions for PHP gallery from folder?
考虑到我从文件夹(g-images /)创建画廊并使用字幕的文件名的工作代码,我如何从此类字幕中排除* .jpg(即* .png,*)以外的其他文件扩展名.gif)?
目前,唯一被删除的扩展名是* .jpg。 如果是其他扩展名,则将其保留为图像标题的一部分...
帮助,这里是新手:-)
<?php
$imglist = array();
$img_folder = "g-images/";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and adds them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)){
$imglist[] = $file;
}
}
closedir($imgs->handle);
//display image
foreach($imglist as $image) {
echo '<li><a href="'.$img_folder.$image.'" target="zoomed"><img src="timthumb.php?src='.$img_folder.$image.'&a=r&h=260" />';
echo '<p>'.str_replace('.jpg', ' ', str_replace('name', 'Name', $image)).'</p></a></li>';
}
?>
echo '<p>'.str_replace(array('.jpg', '.png', '.gif'), ' ', str_replace('name', 'Name', $image)).'</p></a></li>';
在此处了解如何使用str_replace
: http : str_replace
尝试替换此行:
$imglist[] = $file;
与:
$imglist[] = substr( $file, 0, strrpos( $file, '.' ) );
这将在文件名进入数组之前将文件扩展名关闭,并且将与任何扩展名一起使用(例如,将来在将来要支持* .tiff时,不必继续向该数组添加扩展名。 )
更新资料
您可以通过使数组入口数组本身来跟踪文件扩展名:
$position = strrpos( $file, '.' );
$imglist[] = array(
'filename' => substr( $file, 0, $pos ),
'extension' => substr( $file, $pos ),
);
另外,请参考此问题 ,以获得确定文件是否为映像的更好方法。
在您的foreach循环中,您可以执行以下操作
foreach($imglist as $image) {
echo '<li><a href="'.$img_folder.$image.'" target="zoomed"><img src="timthumb.php?src='.$img_folder.$image.'&a=r&h=260" />';
// Explode the image name
$arr = explode('.', $image);
if(isset($arr[0]){
// Get the first element of the array
$imageName = $arr[0];
echo '<p>'.str_replace('name', 'Name', $imageName).'</p></a></li>';
}
}
实现此目的的PHP技巧是explode()名称,将名称分隔在.
是。 然后,我们将array_pop()扩展名关闭,并将其内插回字符串中:
$file_array = explode(".",$image);
$extension = array_pop($file_array);
$filename = implode($file_array);
$filename = ucfirst($filename);
var_dump($filename);
我假设使用此代码
str_replace('name', 'Name', $image)
您要大写字符串吗? 您的代码要做的是查找文字字符串“ name”,如果找到,则将其替换为文字字符串“ Name” ...要大写想要的字符串$ title的首字母:
$capitalizedTitle = ucfirst($title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.