How to get file extension in php?

This post, we helps you get file extension.

1. Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
function getFileExtension($str) {
if ($str != null) {
$array = array();
$array = explode(".", $str);
$a = count($array);
return $array[$a -1];
}
return "";
}
$string = "soltuts.pdf";
$string1 = "soltuts.csv";
echo getFileExtension($string);
echo "<br>";
echo getFileExtension($string1);
?>
<?php function getFileExtension($str) { if ($str != null) { $array = array(); $array = explode(".", $str); $a = count($array); return $array[$a -1]; } return ""; } $string = "soltuts.pdf"; $string1 = "soltuts.csv"; echo getFileExtension($string); echo "<br>"; echo getFileExtension($string1); ?>
    <?php 	
    function getFileExtension($str) { 
     if ($str != null) { 
	 $array = array();
     $array = explode(".", $str);
	 $a = count($array);
	 return $array[$a -1];
     } 
     return ""; 
    } 
	$string = "soltuts.pdf";
	$string1 = "soltuts.csv";
     echo getFileExtension($string);
	 echo "<br>";
     echo getFileExtension($string1);
    ?> 

2. Example:

Input:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
soltuts.pdf
soltuts.csv
soltuts.pdf soltuts.csv
soltuts.pdf
soltuts.csv

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pdf
csv
pdf csv
pdf
csv