Home > headlines > PHP pathinfo function

PHP pathinfo function

不言
Release: 2020-08-01 10:07:00
Original
3309 people have browsed it

pathinfo Introduction

Function: Returns file path information

Syntax

pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] ) : mixed
Copy after login

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;
?>
Copy after login

Output result:

/libs/models
user_model.php
php
user_model
Copy after login

Usage example two

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

var_dump($basename, $dirname);
?>
Copy after login

Output result:

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

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;

?>
Copy after login

Output result:


article_model.php
article_model
php
Copy after login

[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?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template