How do I get the filename suffix with php

  • 2020-06-12 08:35:09
  • OfStack

php Gets file suffix name (format file)

/ / method 1:


<?php   
  function  extend_1( $file_name )   
{   
  $retval =  ""  ;   
  $pt = strrpos ( $file_name ,   " . "  );   
  if  ( $pt )  $retval = substr ( $file_name ,  $pt +1,  strlen ( $file_name ) -  $pt );   
  return  ( $retval );   
}   

2 / / method

function  extend_2( $file_name )   
{   
$extend  =  pathinfo ( $file_name );   
$extend  =  strtolower ( $extend [ "extension" ]);   
return   $extend ;   
}   

3 / / method

function  extend_3( $file_name )   
{   
$extend  = explode (  " . "   ,  $file_name );   
$va = count ( $extend )-1;   
return   $extend [ $va ];   
}   

4 / / method

function  getFileExt( $file_name )   
{   
while ( $dot  =  strpos ( $file_name ,   " . "  ))   
{   
$file_name  =  substr ( $file_name ,  $dot +1);   
}   
return   $file_name ;   
}   
?>   

In addition:
PHP pathinfo () function
PHP Filesystem function
Definition and usage
The pathinfo() function returns information about the file path as an array.
grammar
pathinfo(path,options)
parameter
describe
path
A necessity. Specify the path to check.
process_sections
Optional. Specifies the array elements to return. The default is all.
Possible values:
The PATHINFO_DIRNAME wok returns dirname only
The PATHINFO_BASENAME wok returns basename only
The PATHINFO_EXTENSION wok returns extension only
instructions
pathinfo() returns an associative array containing information about path.
Include the following array elements:
[dirname]
[basename]
[extension]
Hints and comments
Comment: The pathinfo() function returns a string if all cells are not required.
example
Example 1

<?phpprint_r( pathinfo (  " /testweb/test.txt "  ));?>   
//  Output:    
// Array([dirname] => /testweb[basename] => test.txt[extension] => txt)    

Example 2

<?phpprint_r( pathinfo (  " /testweb/test.txt "  ,PATHINFO_BASENAME));?>   
//  Output:    
// test.txt 


Related articles: