Home  >  Article  >  Backend Development  >  How to get the extension of url in php

How to get the extension of url in php

王林
王林Original
2019-12-02 10:08:123164browse

How to get the extension of url in php

PHP Gets the extension of the file in the url address

$url = 'http://www.abc.com/ab/cd/e.php?id=1&data=2#laowen';
echo pathinfo( parse_url($url)['path'] )['extension'];
echo pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION );

Parsing:

parse_url($url);

parse_url() This function parses the url and returns its components; returns an associative array

  scheme          方案;如 http
  host        域名
  port
  user        用户
  pass    
  path        路径
  query        在问号?之后
  fragment        在散列符号#之后

eg: print_r( $url );

Array( 
  [scheme] = >http 
  [host] => www.abc.com 
  [path] => /ab/cd/e.php 
  [query] => id=1&data=2 
  [fragment] => laowen 
)

Extension:

打印解析url              var_dump(parse_url($url));
打印url的scheme          var_dump(parse_url($url, PHP_URL_SCHEME));
打印url的user          var_dump(parse_url($url, PHP_URL_USER));
打印url的pass          var_dump(parse_url($url, PHP_URL_PASS));
打印url的host          var_dump(parse_url($url, PHP_URL_HOST));
打印url的port          var_dump(parse_url($url, PHP_URL_PORT));
打印url的path          var_dump(parse_url($url, PHP_URL_PATH));
打印url的query          var_dump(parse_url($url, PHP_URL_QUERY));
打印url的fragment      var_dump(parse_url($url, PHP_URL_FRAGMENT));
pathinfo('/ab/cd/e.php');

pathinfo()Parse the file path and return its components; return an associative array

dirname          文件路径
basename      文件名+扩展名
extension      最后一个扩展名
filename      文件名

#eg: print_r( pathinfo('/ab/cd/e.php') );

Array(
  [dirname] => /ab/cd
  [basename] => e.php
  [extension] => php
  [filename] => e
)

Extension:

打印解析路径              var_dump( pathinfo($path) );
打印路径的父级路径            var_dump( pathinfo($path, PATHINFO_DIRNAME) );
打印路径的尾名              var_dump( pathinfo($path, PATHINFO_BASENAME) );
打印路径的最后的扩展名      var_dump( pathinfo($path, PATHINFO_EXTENSION) );
打印路径的文件的名字      var_dump( pathinfo($path, PATHINFO_FILENAME) );

Recommended related articles and tutorials:Getting started with php

The above is the detailed content of How to get the extension of url 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