Solved

How can I display part of the filename?

Posted on 2011-03-16
8
322 Views
Last Modified: 2012-05-11
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!

<?
	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>';
	}
	?>

Open in new window

0
Comment
Question by:Computer Guy
8 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35145102
// variable "theString" has the contents "filename.mp3"
filenameOnly = theString.substring(0,theString.lastIndexOf(".");
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35145115
Example
<?php
$path = "/testweb/home.php";

//Show filename with file extension
echo basename($path) ."<br/>";

//Show filename without file extension
echo basename($path,".php");
?>

Open in new window


The output of the code above will be:
home.php
home


Reference:-
http://www.w3schools.com/PHP/func_filesystem_basename.asp
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 35145192
Could I please have an example with my code?
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 35146127
Try this in your code:

$photoName = substr($photos[$i],strrchr($photos[$i],"."),strlen($photos[$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)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 35146723
Hi,

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>';
	}
?>

Open in new window

0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35147160
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>';
	}
	?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
ID: 35147177
sorry i used .php instead of .jpg

Correct Code:-

<?
	for ($i = $start; $i < $limit; $i++) {
		if (!isset($photos[$i])) break; // No more photos
                $photoNumber=basename($photos[$i],".jpg");
		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>';
	}
	?>

Open in new window

0
 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 35155750
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.

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now