iBlog random » snippets http://blog.zurka.us Just another WordPress weblog Tue, 28 May 2013 20:28:47 +0000 en-US hourly 1 http://wordpress.org/?v=3.6 2 Methods for Finding File Extensions in PHP http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/ http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/#comments Wed, 25 Feb 2009 17:25:31 +0000 nasal http://blog.zurka.us/?p=859

Two very simple methods for getting to know which is the file extension of the file, using two very simple PHP functions.

1. Method: function end

The end function finds the last member of an array. So if we want to get the file extension, we will first explode our filename at the dots (.) and then use this function to get the last array object, which will obviouly be the extension.

Example:

<?php
$file = 'something.jpg';
$explode = explode(".", $file);
echo end($explode); // will return jpg
?>

Or shortened:

<?php
$file = 'something.jpg';
echo end(explode(".", $file)); // will return jpg
?>

2. Method: function strrchr

This function finds the last occurrence of a character in a string. Using this function, our php code would look like this:

<?php
$file = 'something.jpg';
echo substr(strrchr($file,"."),1);
?>

Easy!

]]>
http://blog.zurka.us/2-methods-for-finding-file-extensions-in-php/feed/ 3