Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strip directory name and extension from file name

Posted on 2010-09-23
6
Medium Priority
?
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

704 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