Getting file extension, how many ways?
Posted: Thu Jul 01, 2004 7:41 am
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.
Anyone else who is bored have any more?
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;
?>