doupang9614 2014-01-08 20:54
浏览 50
已采纳

如何在文件夹中使用文件名作为PHP库的标题时排除文件扩展名?

Considering my working code for creating a gallery from a folder (g-images/) and using the file names for captions, how do I go about to exclude from such captions other file extensions other than *.jpg (i.e. *.png, *.gif)?

at the moment the only extension that gets removed is *.jpg. if it's any other extension it's kept as part of the caption for the image...

HELP, total newbie here :-)

<?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>';
 }
?>
  • 写回答

5条回答 默认 最新

  • dongyu4908 2014-01-08 21:00
    关注
    echo '<p>'.str_replace(array('.jpg', '.png', '.gif'), ' ', str_replace('name', 'Name', $image)).'</p></a></li>';
    

    Learn how to use str_replace here: http://www.php.net/manual/en/function.str-replace

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据