Home > Article > Backend Development > What are the several ways to get the url extension in php
Obtaining method: 1. Use the "substr(strrchr($url,"."),1)" statement; 2. Use "substr($url,strrpos($url,'.') 1) " statement; 3. Use "pathinfo($url,PATHINFO_EXTENSION)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php gets url extension Name method
Method 1:
<?php $url="http://localhost/user/order.php"; function get_ext1($url){ return substr(strrchr($url,"."),1); } echo get_ext1($url); ?>
Method 2:
<?php $url="http://localhost/user/order.php"; function get_ext2($url){ $p=pathinfo($url);//Array ( [dirname] => http://localhost/user [basename] => order.php [extension] => php [filename] => order ) return $p['extension']; } echo get_ext2($url); ?>
Method 3:
<?php $url="http://localhost/user/order.php"; function get_ext3($url){ return substr($url,strrpos($url,'.')+1); } echo get_ext3($url); ?>
Method 4:
<?php $url="http://localhost/user/order.php"; function get_ext4($url){ $arr=explode('.',$url); return array_pop($arr); } echo get_ext4($url); ?>
Method 5:
<?php $url="http://localhost/user/order.php"; function get_ext5($url){ return pathinfo($url,PATHINFO_EXTENSION); } echo get_ext5($url); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the several ways to get the url extension in php. For more information, please follow other related articles on the PHP Chinese website!