Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

strip directory name and extension from file name

Posted on 2010-09-23
6
Medium Priority
?
400 Views
Last Modified: 2012-06-27
Hi,

I use this script to get pictures from a folder and display them.

I would like to strip the directory-name and the extension from "$num" (IN THE TITLE TAG) so the title will have the picture name only.

thanks a lot!
<?php 
						$files = glob("pictures/*.*"); 
						$files = array_reverse($files); for ($i=0; $i<count($files); $i++) { $num = $files[$i]; 
						echo "<a href='".$num."' rel='shadowbox[Mobile]' title=\"". $num ." | Navigate with the &#8249;&#8212; and &#8212;&#8250; arrowbuttons.\"><img src='imgsize.php?h=75&img=".$num."' style='border: solid 1px black;'></a>&nbsp;&nbsp;";}
						 ?>

Open in new window

0
Comment
Question by:peps03
6 Comments
 
LVL 3

Expert Comment

by:guitar7man
ID: 33744385
Are there further directories within the pictures folder?
0
 
LVL 7

Expert Comment

by:ziceva
ID: 33744463
This should do the trick:

$path = "/home/httpd/html/index.php";
$file = basename($path);
$file=substr($file,0,strrpos($file,'.'));

echo $file;
0
 
LVL 3

Expert Comment

by:wuff
ID: 33744629
if all files have the same extension, e.g. ".jpg" you can provide this to basename() and you get the filename without extension.

$path = "/mydir/images/myimage.jpg";
$filename = basename($path, '.jpg');
echo $filename;

gives this output:

myimage
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:peps03
ID: 33744639
thanks ziceva, but i dont really get it.

how do i fit that in the code above? --> title=\"". $num ."

the num has to be stripped from dir-name and .jpg

@quitar7man: no

thanks!
0
 
LVL 7

Accepted Solution

by:
ziceva earned 2000 total points
ID: 33744692
like so:

$files = glob("pictures/*.*");
$files = array_reverse($files);
for ($i=0; $i<count($files); $i++)
{
 $num = $files[$i];
 $short_num = basename($num);
 $short_num = substr($short_num,0,strrpos($short_num,'.'));
 echo "<a href='".$num."' rel='shadowbox[Mobile]' title=\"". $short_num ." | Navigate with the &#8249;&#8212; and &#8212;&#8250; arrowbuttons.\"><img src='imgsize.php?h=75&img=".$num."' style='border: solid 1px black;'></a>&nbsp;&nbsp;";
}
0
 

Author Closing Comment

by:peps03
ID: 33744828
Thanks a lot Ziceva!

It works great!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

926 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