Computer Guy
asked on
How can I display part of the filename?
Hi,
I have a little photo gallery.
All photos are numbered starting with 01 (leading 0 included for photos 1-9)
01.jpg, 02.jpg, 03.jpg, 04.jpg, 05.jpg, 06.jpg, 07.jpg, 08.jpg, 09.jpg, 10.jpg.......
I want to display that number in the "DISPLAY PHOTO NUMBER HERE (02 FORMAT)" area of the code attached.
Just the number "02", with out the file extension.
How can I do that?
Thanks!
I have a little photo gallery.
All photos are numbered starting with 01 (leading 0 included for photos 1-9)
01.jpg, 02.jpg, 03.jpg, 04.jpg, 05.jpg, 06.jpg, 07.jpg, 08.jpg, 09.jpg, 10.jpg.......
I want to display that number in the "DISPLAY PHOTO NUMBER HERE (02 FORMAT)" area of the code attached.
Just the number "02", with out the file extension.
How can I do that?
Thanks!
<?
for ($i = $start; $i < $limit; $i++) {
if (!isset($photos[$i])) break; // No more photos
echo '<a class="vlightbox1" href="' . $eventinfo['photo_path_url'] . '/web/' . $photos[$i] . '"><img src="' . $eventinfo['photo_path_url'] . '/thumbs/' . $photos[$i] . '" alt="Photo" border="0"/>'. $product_title .'DISPLAY PHOTO NUMBER HERE</a>';
}
?>
Example
The output of the code above will be:
home.php
home
Reference:-
http://www.w3schools.com/P HP/func_fi lesystem_b asename.as p
<?php
$path = "/testweb/home.php";
//Show filename with file extension
echo basename($path) ."<br/>";
//Show filename without file extension
echo basename($path,".php");
?>
The output of the code above will be:
home.php
home
Reference:-
http://www.w3schools.com/P
ASKER
Could I please have an example with my code?
Try this in your code:
$photoName = substr($photos[$i],strrchr ($photos[$ i],"."),st rlen($phot os[$i]));
I'm not familiar with php, so maybe you'll need to substract 1 from strlen depending on the starting counting value (0 or 1)
$photoName = substr($photos[$i],strrchr
I'm not familiar with php, so maybe you'll need to substract 1 from strlen depending on the starting counting value (0 or 1)
Hi,
Try this,
Try this,
<?php
for ($i = $start; $i < $limit; $i++) {
if (!isset($photos[$i])) break; // No more photos
$lastPosition = strrpos($photos[$i], '.');
$photoNumber = substr($photos[$i], 0, $lastPosition);
echo '<a class="vlightbox1" href="' . $eventinfo['photo_path_url'] . '/web/' . $photos[$i] . '"><img src="' . $eventinfo['photo_path_url'] . '/thumbs/' . $photos[$i] . '" alt="Photo" border="0"/>'. $product_title.$photoNumber.'</a>';
}
?>
using basename function:-
<?
for ($i = $start; $i < $limit; $i++) {
if (!isset($photos[$i])) break; // No more photos
$photoNumber=basename($photos[$i],".php");
echo '<a class="vlightbox1" href="' . $eventinfo['photo_path_url'] . '/web/' . $photos[$i] . '"><img src="' . $eventinfo['photo_path_url'] . '/thumbs/' . $photos[$i] . '" alt="Photo" border="0"/>'. $product_title .$photoNumber.'</a>';
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
The accepted solution will given error file names when the file extensions other than .jpg.
If you only use ".jpg" files you can have mayank joshi code. Otherwise my code is the bestway to get the filename with any extension.
The accepted solution will given error file names when the file extensions other than .jpg.
If you only use ".jpg" files you can have mayank joshi code. Otherwise my code is the bestway to get the filename with any extension.
filenameOnly = theString.substring(0,theS