PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
japaja
Forum Newbie
Posts: 6 Joined: Sun Apr 06, 2003 8:58 am
Post
by japaja » Tue Jul 06, 2004 12:09 pm
Code: Select all
<?php
$imgpath = "thumbs";
$imgpath2 = "fullphoto";
$handle = opendir( "$imgpath" );
$handle2 = opendir( "$imgpath2" );
$imgArray = array();
while($file = readdir($handle))
{
if( $file != "." && $file != ".." )
{
array_push( $imgArray, $file );
}
}
closedir( $handle );
closedir( $handle2 );
mt_srand( (double)microtime( ) * 1000000 );
$randval = mt_rand( 0, sizeof( $imgArray ) - 1 );
echo "<td align=center><a href="$imgpath2/" . $imgArray[ $randval ] . "" onclick="window.open(this.href, 'popupwindow', 'height=605,width=405,scrollbars,resizable'); return false;"><BR><img border=0 SRC="$imgpath/" . $imgArray[ $randval ] . ""><br /> </a></td>";?>
How to get filename without extension? to echo it like image headline....
feyd | Please use Code: Select all
tags when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by
japaja on Wed Jul 07, 2004 8:52 am, edited 1 time in total.
qads
DevNet Resident
Posts: 1199 Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane
Post
by qads » Tue Jul 06, 2004 12:41 pm
japaja
Forum Newbie
Posts: 6 Joined: Sun Apr 06, 2003 8:58 am
Post
by japaja » Wed Jul 07, 2004 8:50 am
Code: Select all
<? echo("<br>"); ?>
<?
$split = explode('.', $imgArray[$randval]);
$filename_without_extension = $split[0];
echo $filename_without_extension;
?>
<? echo("<br>"); ?>
but what if : string.name.jpg ?
SOLUTION IS
Code: Select all
<?echo substr("$imgArray[$randval]", 0, -4); ?>
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Jul 07, 2004 9:41 am
What about files with a four letter extension!?
tiff
jpeg
etc...
Mark
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed Jul 07, 2004 9:49 am
Code: Select all
$just_the_name=explode('.',$filename);
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Jul 07, 2004 10:16 am
patrikG wrote: Code: Select all
$just_the_name=explode('.',$filename);
What about his worry if a filename has a perios in the name!?
mark
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed Jul 07, 2004 10:21 am
then explode won't work and he has to use regex.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jul 07, 2004 12:20 pm
(untested)
Code: Select all
<?php
$name = explode('.',$filename);
if(sizeof($name) == 1)
{
$name = $name[0];
$ext = '';
}
else
{
$ext = array_pop($name);
$name = implode('.',$name);
}
?>
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Wed Jul 07, 2004 1:12 pm
Or of course:
Code: Select all
$filename = substr($file, 0, strrpos($file, '.'));
Now it doesn't matter how long the file extension is or how many other periods exist in the file name.
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Wed Jul 07, 2004 2:48 pm
launchcode wrote: Or of course:
Code: Select all
$filename = substr($file, 0, strrpos($file, '.'));
Now it doesn't matter how long the file extension is or how many other periods exist in the file name.
But if the file has no extension $filename is now empty, whereas feyd's method will set the $name variable with the name of the file regardless if there is a file extension or not (in my mind a better solution).
There is also....
Code: Select all
$name = preg_replace('/\.[^.]+$/', '', $filename);
launchcode
Forum Contributor
Posts: 401 Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:
Post
by launchcode » Wed Jul 07, 2004 4:52 pm
Then a basic check to see if $filename contains a period first will stop the need to fire-up the entire preg engine for such a simple task.
Code: Select all
$pos = strrpos($file, '.');
if ($pos)
{
$filename = substr($file, 0, $pos);
}
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Wed Jul 07, 2004 5:35 pm
Code: Select all
<?php
function exectime($_start, $_end, $_precision = 4)
{
list($usec, $sec) = explode(' ', $_start);
$start_time = ((float)$usec + (float)$sec);
list($usec, $sec) = explode(' ', $_end);
$end_time = ((float)$usec + (float)$sec);
return round(($end_time - $start_time), $_precision);
}
$filename = 'some.file.txt';
$start_preg = microtime();
for ($i = 0; $i < 1000; $i++)
{
$name = preg_replace('/\.[^.]+$/', '', $filename);
}
$end_preg = microtime();
$start_substr = microtime();
for ($i = 0; $i < 1000; $i++)
{
$pos = strrpos($filename, '.');
if ($pos)
{
$name = substr($filename, 0, $pos);
}
}
$end_substr = microtime();
echo 'Time with preg = ' . exectime($start_preg, $end_preg) . "\x0a";
echo 'Time with substr = ' . exectime($start_substr, $end_substr) . "\x0a";
?>
Outputs (average)......
Code: Select all
Time with preg = 0.1374
Time with substr = 0.2273
japaja
Forum Newbie
Posts: 6 Joined: Sun Apr 06, 2003 8:58 am
Post
by japaja » Thu Jul 08, 2004 10:21 am
Bech100 wrote: What about files with a four letter extension!?
tiff
jpeg
etc...
Mark
Code: Select all
<?
$fileWithoutExtension = substr($imgArray[$randval],0,strrpos($imgArray[$randval],".") + 0);
echo $fileWithoutExtension;
?>