The extension is the string after the last (.) in the file path. The pathinfo function can be used to return the file path information, so this article will introduce to you how to obtain the extension by the pathinfo function in PHP.
How to use the pathinfo function
Use the pathinfo function to return information about the file path.
mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
In path, specify the path to be checked.
In options, you can use constants to specify the elements to be returned. If not specified, all elements are returned.
If options are omitted, the return value is an associative array containing PATHINFO_DIRNAME, PATHINFO_BASENAME and PATHINFO_EXTENSION or PATHINFO_FILENAME. To get the extension, the key will get the value of the extension element.
If only PATHINFO_EXTENSION is specified in options, you can only get the extension string.
Let’s look at a specific example
Get the file path extension through the pathinfo function.
The code is as follows
<?How to get the extension using How to get the extension using phps pathinfo functions pathinfo function $path_parts = pathinfo('/www/htdocs/inc/lib.inc.How to get the extension using How to get the extension using phps pathinfo functions pathinfo function'); echo $path_parts['extension'] . PHP_EOL; $extension = pathinfo('/www/htdocs/inc/lib.inc.How to get the extension using How to get the extension using phps pathinfo functions pathinfo function', PATHINFO_EXTENSION); echo $extension . PHP_EOL;
The execution result is displayed as follows:
This article ends here It’s all over. For more exciting content, you can pay attention to the relevant column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to get the extension using php's pathinfo function. For more information, please follow other related articles on the PHP Chinese website!