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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程