Page 1 of 1

Getting file extension, how many ways?

Posted: Thu Jul 01, 2004 7:41 am
by redmonkey
I was bored and decided to see how many ways I could come up with to get the file extension for a given filename using only a true single line of code but still have a good amount of error checking and no repition i.e. reversing an array twice. I stopped at five as I found something more productive to do :). These all are single lines of code, but probably wrapped due to length of lines.

Code: Select all

<?php

$filename = "i.am.bored.html";

$ext = is_string($filename) ? preg_match('/\.(\w+)$/', trim($filename), $ext) ? $ext[1] : false : false;

$ext = is_string($filename) ? strpos($filename, '.') !== false ? (!($ext = trim(end(explode('.', $filename))))) ? false : $ext : false : false;

$ext = is_string($filename) ? strpos($filename, '.') !== false ? (!($ext = trim(array_pop(explode('.', $filename))))) ? false : $ext : false : false;

$ext = is_string($filename) ? strpos($filename, '.') !== false ? (!($ext = trim(substr($filename, strrpos($filename, '.') + 1)))) ? false : $ext : false : false;

$ext = is_string($filename) ? strpos($filename, '.') !== false ? (!($ext = trim(array_shift(array_reverse(explode('.', $filename)))))) ? false : $ext : false : false;

?>
Anyone else who is bored have any more?

Posted: Thu Jul 01, 2004 7:43 am
by malcolmboston
explode on .

Posted: Thu Jul 01, 2004 7:44 am
by Grim...

Code: Select all

<?php

$ext = "html";

?>
:D

Posted: Thu Jul 01, 2004 7:47 am
by redmonkey
Grim... wrote:

Code: Select all

<?php

$ext = "html";

?>
:D
malcolmboston wrote:explode on .
I think we are missing the point.

Posted: Thu Jul 01, 2004 8:21 am
by JayBird
what about

Code: Select all

$info = pathinfo($filename);
echo $info["extension"]
or

Code: Select all

$extension = substr (strrchr ($filename, "."), 1);
echo $extension;

Mark

Posted: Thu Jul 01, 2004 9:09 am
by redmonkey
Bech100 wrote:what about

Code: Select all

$info = pathinfo($filename);
echo $info&#1111;"extension"]
That's two lines essentially you are saying....

Code: Select all

$ext = pathinfo($filename);
$ext = $ext['extension'];
Bech100 wrote:or

Code: Select all

$extension = substr (strrchr ($filename, "."), 1);
echo $extension;
What if.....

Code: Select all

$filename = "somefile.txt               ";
:wink:

Posted: Thu Jul 01, 2004 9:51 am
by JayBird
redmonkey wrote: What if.....

Code: Select all

$filename = "somefile.txt               ";
:wink:
Well that would be just daft ;)

Posted: Thu Jul 01, 2004 10:06 am
by redmonkey
Bech100 wrote:Well that would be just daft ;)
Well yes, but you have to cater for user stupidity, don't you?

Posted: Thu Jul 01, 2004 12:32 pm
by scorphus
Let me try:

Code: Select all

<?php
$filename = "i.am.bored.html";
$ext = ($ext = pathinfo($filename)) ? (!empty($ext['extension'])) ? trim($ext['extension']) : false : false;
?>
-- Scorphus

Posted: Thu Jul 01, 2004 12:39 pm
by redmonkey
scorphus wrote:Let me try:

Code: Select all

<?php
$filename = "i.am.bored.html";
$ext = ($ext = pathinfo($filename)) ? (!empty($ext['extension'])) ? trim($ext['extension']) : false : false;
?>
-- Scorphus
Yep, that's one for the list.

Posted: Thu Jul 01, 2004 12:42 pm
by markl999

Code: Select all

$ext = trim(end(explode('.', $filename)));

Posted: Thu Jul 01, 2004 12:46 pm
by redmonkey
markl999 wrote:

Code: Select all

$ext = trim(end(explode('.', $filename)));
Already covered in my second example within my first post, only I included more error checking. e.g. what if $filename = "index"