0

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>';
 }
?>
kcullen
  • 5
  • 4

5 Answers5

0
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

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
0

Try replacing this line:

$imglist[] = $file;

with:

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

This will lop off the file extension before the filename makes it into the array, and will work with any extension (you won't have to keep adding extensions to an array if, for example, in the future you want to support *.tiff)

Update

You can keep track of the file extensions by making the array entries arrays themselves:

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

Also, refer to this question for a better way to determine if a file is an image or not.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • The OP uses the entries in `$imglist` to create the file path. You can't remove the extensions before the path is created. – Patrick Q Jan 08 '14 at 21:03
0

Inside your foreach loop you can do the following

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>';
    }
}
Kostis
  • 1,105
  • 8
  • 10
0

A PHP trick to accomplish this is to explode() the name, dividing it where the . is. Then we just array_pop() the extension off, and implode it back into a string:

$file_array = explode(".",$image);
$extension = array_pop($file_array);
$filename = implode($file_array);
$filename = ucfirst($filename);
var_dump($filename);

I assume with this code

str_replace('name', 'Name', $image)

You're trying to capitalize the string? What your code does is look for the literal string "name" and if found, replace it with the literal string "Name"...To capitalize the first letter of string $title you want:

$capitalizedTitle = ucfirst($title);
Digital Chris
  • 6,177
  • 1
  • 20
  • 29
0

http://php.net/pathinfo

pathinfo( '/path/to/file.txt', PATHINFO_FILENAME ) will return just the filename without the extension.

The above will return just file

Hayden
  • 2,082
  • 1
  • 14
  • 18