Home >Backend Development >PHP Problem >What are the several ways to get the url extension in php

What are the several ways to get the url extension in php

青灯夜游
青灯夜游Original
2022-02-10 15:55:413197browse

Obtaining method: 1. Use the "substr(strrchr($url,"."),1)" statement; 2. Use "substr($url,strrpos($url,'.') 1) " statement; 3. Use "pathinfo($url,PATHINFO_EXTENSION)".

What are the several ways to get the url extension in php

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);
?>

What are the several ways to get the url extension in php

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[&#39;extension&#39;];
}
echo get_ext2($url);
?>

Method 3:

<?php
$url="http://localhost/user/order.php";
function get_ext3($url){
	return substr($url,strrpos($url,&#39;.&#39;)+1);
}
echo get_ext3($url);
?>

Method 4:

<?php
$url="http://localhost/user/order.php";
function get_ext4($url){
	$arr=explode(&#39;.&#39;,$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn