Home  >  Article  >  PHP pathinfo function

PHP pathinfo function

不言
不言Original
2018-05-09 15:05:083289browse

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

##pathThe path to be parsed. options
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

If no options are passed in, it will Returns an array containing the following elements: dirname, basename and extension (if any), and filename.

If the path has no extension, no extension elements will be returned.

If the option is present, returns a string containing the requested element.

pathinfo example

Usage example one

<?php
$pathinfo = pathinfo(&#39;/libs/models/user_model.php&#39;);

echo $pathinfo[&#39;dirname&#39;], PHP_EOL;
echo $pathinfo[&#39;basename&#39;], PHP_EOL;
echo $pathinfo[&#39;extension&#39;], PHP_EOL;
echo $pathinfo[&#39;filename&#39;], PHP_EOL;
?>

Output result:

/libs/models
user_model.php
php
user_model

Usage example two

<?php
[ &#39;basename&#39; => $basename, &#39;dirname&#39; => $dirname ] = pathinfo(&#39;/libs/models/article_model.php&#39;);

var_dump($basename, $dirname);
?>

Output result:

string(17) "article_model.php"
string(12) "/libs/models"

Use example three

<?php

echo pathinfo(&#39;/libs/models/article_model.php&#39;, PATHINFO_BASENAME), PHP_EOL;
echo pathinfo(&#39;/libs/models/article_model.php&#39;, PATHINFO_FILENAME), PHP_EOL;
echo pathinfo(&#39;/libs/models/article_model.php&#39;, PATHINFO_EXTENSION), PHP_EOL;

?>

Output result:


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?

route - laravel routing, can pathinfo mode be implemented

javascript - Are there any benefits to pathinfo?

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