Easy Tutorial
❮ Python One And Two Star Programmer Joke 23 ❯

PHP Methods for Obtaining URL Extensions

Category Programming Techniques

The following example demonstrates 5 methods for obtaining URL extensions in PHP:

Example

<?php  
$url="http://www.tutorialpro.org/html/html-tutorial.html";  
// String slicing
function get_ext1($url){  
    return substr(strrchr($url,"."),1);  
}  

// Using pathinfo
function get_ext2($url){  
    $p=pathinfo($url);
    return $p['extension'];  
}  

// String slicing
function get_ext3($url){  
    return substr($url,strrpos($url,'.')+1);  
}  
// Using array_pop
function get_ext4($url){  
    $arr=explode('.',$url);  
    return array_pop($arr);  
} 
// Using pathinfo and its constants 
function get_ext5($url){  
    return pathinfo($url,PATHINFO_EXTENSION);  
}  

echo get_ext1($url) . PHP_EOL;  
echo get_ext2($url) . PHP_EOL;  
echo get_ext3($url) . PHP_EOL;  
echo get_ext4($url) . PHP_EOL;  
echo get_ext5($url) . PHP_EOL;

Test output results are:

html
html
html
html
html

**Click to Share Notes

Cancel

-

-

-

❮ Python One And Two Star Programmer Joke 23 ❯