Usephul

functions.php

Tags
author

Alwin Garside alwin@garsi.de

copyright

2025 The Empaphy Project

license

MIT

Table of Contents

Functions

basename()  : string
Returns the trailing name component of a given path.
dirname()  : string
Returns a parent directory's path.
extension()  : string
Returns the extension component of a given path without the extension.
extension_replace()  : string
Replace the extension of a path with a new value.
filename()  : string
Returns the name component of a given path without the extension.

Functions

basename()

Returns the trailing name component of a given path.

basename(string $path[, string $suffix = '' ]) : string

Given a string containing the path to a file or directory, this function will return the trailing name component.

Note:

basename() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Caution: basename() is locale-aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function. If path contains characters which are invalid for the current locale, the behavior of basename() is undefined.

Parameters
$path : string

A path.

On Windows, both slash (/) and backslash (\) are used as directory separator characters. In other environments, it is the forward slash (/).

$suffix : string = ''

If the name component ends in suffix, this will also be cut off.

Tags
see
dirname()
  • Returns a parent directory's path
see
extension()
  • Returns extension component of a path
see
filename()
  • Returns basename of a path, without extension
see
pathinfo()
  • Returns information about a file path
Return values
string

Returns the base name of the given path.

dirname()

Returns a parent directory's path.

dirname(string $path[, int $levels = 1 ]) : string

Given a string containing the path of a file or directory, this function will return the parent directory's path that is levels up from the current directory.

Note:

dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Caution: On Windows, dirname() assumes the currently set codepage, so for it to see the correct directory name with multibyte character paths, the matching codepage must be set. If path contains characters which are invalid for the current codepage, the behavior of dirname() is undefined.

On other systems, dirname() assumes path to be encoded in an ASCII compatible encoding. Otherwise, the behavior of the function is undefined.

Parameters
$path : string

A path.

On Windows, both slash (/) and backslash (\) are used as directory separator characters. In other environments, it is the forward slash (/).

$levels : int = 1

The number of parent directories to go up.

This must be an integer greater than 0.

Tags
see
basename()
  • Returns trailing name component of a path
see
pathinfo()
  • Returns information about a file path
see
realpath()
  • Returns canonicalized absolute pathname
Return values
string

Returns the path of a parent directory. If there are no slashes in path, a dot ('.') is returned, indicating the current directory. Otherwise, the returned string is path with any trailing /component removed.

Caution: Be careful when using this function in a loop that can reach the top-level directory as this can result in an infinite loop.

<?php
dirname('.');    // Will return '.'.
dirname('/');    // Will return `\` on Windows and '/' on *nix.
dirname('\\');   // Will return `\` on Windows and '.' on *nix.
dirname('C:\\'); // Will return 'C:\' on Windows and '.' on *nix.
?>

extension()

Returns the extension component of a given path without the extension.

extension(string $path) : string

Given a string containing the path to a file or directory, this function will return the extension component.

extension('/root/dir/sub/name.suf.ext'); // returns 'ext'
extension('/root/dir/sub/name.ext');     // returns 'ext'
extension('/root/dir/sub/.ext');         // returns 'ext'
extension('/root/dir/sub/name');         // returns ''
extension('/root/dir/sub/');             // returns ''

Note:

If the path has more than one extension, extension() returns only the last one.

Note:

extension() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Caution extension() is locale-aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.

Parameters
$path : string

A path.

Tags
see
dirname()
  • Returns a parent directory's path
see
basename()
  • Returns trailing name component of a path
see
filename()
  • Returns basename of a path, without extension
see
pathinfo()
  • Returns information about a file path
Return values
string

The extension component of path. If the path has no extension component, an empty string is returned.

extension_replace()

Replace the extension of a path with a new value.

extension_replace(string $path[, string $replacement = '' ][, string $suffix = '' ]) : string

If replacement is left empty, the extension is removed from path, including the seperating period (.).

Parameters
$path : string

A path.

$replacement : string = ''

The replacement extension. If empty, the extension is removed.

$suffix : string = ''

If the name component ends in suffix, this will also be replaced.

Return values
string

Returns path with any extension (and optionally suffix) replaced with replacement.

filename()

Returns the name component of a given path without the extension.

filename(string $path[, string $suffix = '' ]) : string

Given a string containing the path to a file or directory, this function will return the trailing name component without the extension.

filename('/root/dir/sub/name.suf.ext'); // returns 'name.suf'
filename('/root/dir/sub/name.ext');     // returns 'name'
filename('/root/dir/sub/name');         // returns 'name'
filename('/root/dir/sub/.ext');         // returns ''
filename('/root/dir/sub/');             // returns 'sub'

filename('/root/dir/sub/name.suf.ext', '.ext') // returns 'name.suf'
filename('/root/dir/sub/name.suf.ext', '.suf') // returns 'name'
filename('/root/dir/sub/name.suf.suf', '.suf') // returns 'name'
filename('/root/dir/sub/name.ext',     '.suf') // returns 'name'
filename('/root/dir/sub/name.suf',     '.suf') // returns 'name'
filename('/root/dir/sub/name',         '.suf') // returns 'name'
filename('/root/dir/sub/.suf.ext',     '.suf') // returns ''
filename('/root/dir/sub/',             '.suf') // returns 'sub'
filename('/root/dir/sub/',             'sub')  // returns ''

Note:

If the path has more than one extension, filename() only strips the last one.

Note:

filename() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".

Caution filename() is locale-aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function.

Parameters
$path : string

A path.

$suffix : string = ''

If the name component ends in suffix, this will also be cut off.

Tags
see
dirname()
  • Returns a parent directory's path
see
basename()
  • Returns trailing name component of a path
see
extension()
  • Returns extension component of a path
see
pathinfo()
  • Returns information about a file path
Return values
string

Returns the name component of path.


        
On this page

Search results