pathinfo Introduction
Function: Returns file path information
Syntax:
pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] ) : mixed
Returns an associative array containing path information. Whether an associative array or a string is returned depends on options.
pathinfo parameter
The path to be parsed. | |
If specified, the specified element will be returned; they include: PATHINFO_DIRNAME, PATHINFO_BASENAME and PATHINFO_EXTENSION or PATHINFO_FILENAME. If options are not specified, the default is to return all units. |
pathinfo return value
pathinfo example
Usage example one
<?php $pathinfo = pathinfo('/libs/models/user_model.php'); echo $pathinfo['dirname'], PHP_EOL; echo $pathinfo['basename'], PHP_EOL; echo $pathinfo['extension'], PHP_EOL; echo $pathinfo['filename'], PHP_EOL; ?>
/libs/models user_model.php php user_model
Usage example two
<?php [ 'basename' => $basename, 'dirname' => $dirname ] = pathinfo('/libs/models/article_model.php'); var_dump($basename, $dirname); ?>
string(17) "article_model.php" string(12) "/libs/models"
Use example three
<?php echo pathinfo('/libs/models/article_model.php', PATHINFO_BASENAME), PHP_EOL; echo pathinfo('/libs/models/article_model.php', PATHINFO_FILENAME), PHP_EOL; echo pathinfo('/libs/models/article_model.php', PATHINFO_EXTENSION), PHP_EOL; ?>
article_model.php article_model php
[Related Q&A recommendations]:
Some questions about building a LEMP environment
.htaccess hides index.php and uses pathinfo to report resource file path errors without reporting errors
laravel - Is the PATHINFO mode unique to thinkphp?