【问题标题】:how to exclude file extensions when using file names as captions for PHP gallery from folder?使用文件名作为文件夹中 PHP 库的标题时如何排除文件扩展名?
【发布时间】:2014-01-27 04:22:03
【问题描述】:

考虑到我从文件夹 (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>';
 }
?>

【问题讨论】:

  • 感谢大家的帮助!我真的很感激,干杯! :-)

标签: php gallery caption


【解决方案1】:
echo '<p>'.str_replace(array('.jpg', '.png', '.gif'), ' ', str_replace('name', 'Name', $image)).'</p></a></li>';

在此处了解如何使用str_replace:http://www.php.net/manual/en/function.str-replace

【讨论】:

    【解决方案2】:

    在您的 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>';
        }
    }
    

    【讨论】:

      【解决方案3】:

      尝试替换这一行:

      $imglist[] = $file;
      

      与:

      $imglist[] = substr( $file, 0, strrpos( $file, '.' ) );
      

      这将在文件名进入数组之前删除文件扩展名,并且适用于任何扩展名(例如,如果将来您想要支持*.tiff)

      更新

      您可以通过将数组条目本身设为数组来跟踪文件扩展名:

      $position = strrpos( $file, '.' );
      $imglist[] = array( 
          'filename' => substr( $file, 0, $pos ), 
          'extension' => substr( $file, $pos ), 
      );
      

      另外,请参阅this question 以更好地确定文件是否为图像。

      【讨论】:

      • OP 使用$imglist 中的条目来创建文件路径。在创建路径之前,您无法删除扩展。
      【解决方案4】:

      实现这一点的一个 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);
      

      【讨论】:

        【解决方案5】:

        http://php.net/pathinfo

        pathinfo( '/path/to/file.txt', PATHINFO_FILENAME ) 将只返回不带扩展名的文件名。

        上面将只返回file

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-14
          • 1970-01-01
          • 1970-01-01
          • 2014-04-10
          • 2016-04-27
          • 1970-01-01
          • 2015-10-16
          • 1970-01-01
          相关资源
          最近更新 更多