Solved

strip directory name and extension from file name

Posted on 2010-09-23
6
356 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 look for a specific file type in a local or remote server directory using PHP.

911 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

26 Experts available now in Live!

Get 1:1 Help Now