Jump to content


Photo

Reading an extension...


  • Please log in to reply
6 replies to this topic

#1 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 19 October 2006 - 11:29 AM

Hi there all

I'm treing to read a file extention but don't know how.... it reads the file name and all but just cant get it to read the .jpg extension.
Any idees?

Thanx

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 11:36 AM

You can do it like this:
$var = explode('.',$filename);
$extension = $var[count($var)-1];
echo $extension;


#3 GremlinP1R

GremlinP1R
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts
  • LocationSouth Africa

Posted 19 October 2006 - 12:07 PM

Thanx it works

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 19 October 2006 - 01:00 PM

or, an alternate method:
<?php
$ext = substr($filename, strrpos('.', $filename));
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 19 October 2006 - 01:06 PM

or, an alternate method:

<?php
$ext = substr($filename, strrpos('.', $filename));
?>


Doesn't work.
<?php
$filename = "mysql.db.php";
$ext = substr($filename, strrpos('.', $filename));
echo $ext;
?>
outputs

mysql.db.php



#6 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 19 October 2006 - 01:14 PM

Doesn't work.


my bad, i switched the arguments for strrpos:
<?php
$filename = "mysql.db.php";
$ext = substr($filename, strrpos($filename, '.')+1);
echo $ext;
?>

you could also just use a preg_match:
<?php
$filename = "mysql.db.php";
preg_match('|\.([a-z]+)$|i', $filename, $match);
$ext = $match[1];
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#7 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 19 October 2006 - 01:47 PM

Just another way...

$ext = str_replace ( '.', '', strrchr ( $file, '.' ) );


me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users