Page 1 of 1

[SOLVED] How to get filename without extension?

Posted: Tue Jul 06, 2004 12:09 pm
by japaja

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]

Posted: Tue Jul 06, 2004 12:41 pm
by qads

Posted: Wed Jul 07, 2004 8:50 am
by japaja

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); ?>

Posted: Wed Jul 07, 2004 9:41 am
by JayBird
What about files with a four letter extension!?

tiff
jpeg
etc...

Mark

Posted: Wed Jul 07, 2004 9:49 am
by patrikG

Code: Select all

$just_the_name=explode('.',$filename);

Posted: Wed Jul 07, 2004 10:16 am
by JayBird
patrikG wrote:

Code: Select all

$just_the_name=explode('.',$filename);
What about his worry if a filename has a perios in the name!?

mark

Posted: Wed Jul 07, 2004 10:21 am
by patrikG
then explode won't work and he has to use regex.

Posted: Wed Jul 07, 2004 12:20 pm
by feyd
(untested)

Code: Select all

<?php

$name = explode('.',$filename);
if(sizeof($name) == 1)
{
  $name = $name[0];
  $ext = '';
}
else
{
  $ext = array_pop($name);
  $name = implode('.',$name);
}

?>

Posted: Wed Jul 07, 2004 1:12 pm
by launchcode
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.

Posted: Wed Jul 07, 2004 2:48 pm
by redmonkey
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);

Posted: Wed Jul 07, 2004 4:52 pm
by launchcode
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); 
}

Posted: Wed Jul 07, 2004 5:35 pm
by redmonkey

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

Posted: Thu Jul 08, 2004 10:21 am
by japaja
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;
?>