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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题