Install via yarn add locutus and import:
import { pathinfo } from 'locutus/php/filesystem/pathinfo'.
Or with CommonJS: const { pathinfo } = require('locutus/php/filesystem/pathinfo')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559
The way the bitwise arguments are handled allows for greater flexibility
& compatability. We might even standardize this
code and use a similar approach for
other bitwise PHP functions
Locutus tries very hard to stay away from a core.js
file with global dependencies, because we like
that you can just take a couple of functions and be on your way.
But by way we implemented this function,
if you want you can still declare the PATHINFO_*
yourself, and then you can use:
pathinfo(‘/www/index.html’, PATHINFO_BASENAME | PATHINFO_EXTENSION);
which makes it fully compliant with PHP syntax.
Dependencies
This function uses the following Locutus functions:
exportfunctionpathinfo(path: string, options?: PathInfoOptions): PathInfoMap | string | false { // discuss at: https://locutus.io/php/pathinfo/ // original by: Nate // revised by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // improved by: Dmitry Gorelenkov // input by: Timo // note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559 // note 1: The way the bitwise arguments are handled allows for greater flexibility // note 1: & compatability. We might even standardize this // note 1: code and use a similar approach for // note 1: other bitwise PHP functions // note 1: Locutus tries very hard to stay away from a core.js // note 1: file with global dependencies, because we like // note 1: that you can just take a couple of functions and be on your way. // note 1: But by way we implemented this function, // note 1: if you want you can still declare the PATHINFO_* // note 1: yourself, and then you can use: // note 1: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION); // note 1: which makes it fully compliant with PHP syntax. // example 1: pathinfo('/www/htdocs/index.html', 1) // returns 1: '/www/htdocs' // example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME') // returns 2: 'index.html' // example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION') // returns 3: 'html' // example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME') // returns 4: 'index' // example 5: pathinfo('/www/htdocs/index.html', 2 | 4) // returns 5: {basename: 'index.html', extension: 'html'} // example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL') // returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'} // example 7: pathinfo('/www/htdocs/index.html') // returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
exportfunctionpathinfo(path, options) { // discuss at: https://locutus.io/php/pathinfo/ // original by: Nate // revised by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // improved by: Dmitry Gorelenkov // input by: Timo // note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559 // note 1: The way the bitwise arguments are handled allows for greater flexibility // note 1: & compatability. We might even standardize this // note 1: code and use a similar approach for // note 1: other bitwise PHP functions // note 1: Locutus tries very hard to stay away from a core.js // note 1: file with global dependencies, because we like // note 1: that you can just take a couple of functions and be on your way. // note 1: But by way we implemented this function, // note 1: if you want you can still declare the PATHINFO_* // note 1: yourself, and then you can use: // note 1: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION); // note 1: which makes it fully compliant with PHP syntax. // example 1: pathinfo('/www/htdocs/index.html', 1) // returns 1: '/www/htdocs' // example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME') // returns 2: 'index.html' // example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION') // returns 3: 'html' // example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME') // returns 4: 'index' // example 5: pathinfo('/www/htdocs/index.html', 2 | 4) // returns 5: {basename: 'index.html', extension: 'html'} // example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL') // returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'} // example 7: pathinfo('/www/htdocs/index.html') // returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
const tmpArr = {} let basenameValue = null let extensionValue = null let filenameValue = null
functionpathinfo(path: string, options?: PathInfoOptions): PathInfoMap | string | false { // discuss at: https://locutus.io/php/pathinfo/ // original by: Nate // revised by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // improved by: Dmitry Gorelenkov // input by: Timo // note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559 // note 1: The way the bitwise arguments are handled allows for greater flexibility // note 1: & compatability. We might even standardize this // note 1: code and use a similar approach for // note 1: other bitwise PHP functions // note 1: Locutus tries very hard to stay away from a core.js // note 1: file with global dependencies, because we like // note 1: that you can just take a couple of functions and be on your way. // note 1: But by way we implemented this function, // note 1: if you want you can still declare the PATHINFO_* // note 1: yourself, and then you can use: // note 1: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION); // note 1: which makes it fully compliant with PHP syntax. // example 1: pathinfo('/www/htdocs/index.html', 1) // returns 1: '/www/htdocs' // example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME') // returns 2: 'index.html' // example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION') // returns 3: 'html' // example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME') // returns 4: 'index' // example 5: pathinfo('/www/htdocs/index.html', 2 | 4) // returns 5: {basename: 'index.html', extension: 'html'} // example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL') // returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'} // example 7: pathinfo('/www/htdocs/index.html') // returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
// If array contains only 1 element: return string const values = Object.values(tmpArr).filter((value): value is string => typeof value === 'string') if (values.length === 1) { return values[0] ?? '' }
// Return full-blown array return tmpArr }
// php/filesystem/basename (Locutus dependency module) functionbasename(path, suffix) { // discuss at: https://locutus.io/php/basename/ // parity verified: PHP 8.3 // original by: Kevin van Zonneveld (https://kvz.io) // improved by: Ash Searle (https://hexmen.com/blog/) // improved by: Lincoln Ramsay // improved by: djmix // improved by: Dmitry Gorelenkov // example 1: basename('/www/site/home.htm', '.htm') // returns 1: 'home' // example 2: basename('ecra.php?p=1') // returns 2: 'ecra.php?p=1' // example 3: basename('/some/path/') // returns 3: 'path' // example 4: basename('/some/path_ext.ext/','.ext') // returns 4: 'path_ext'
let b = path if (b.endsWith('/') || b.endsWith('\\')) { b = b.slice(0, -1) }
b = b.replace(/^.*[/\\]/g, '')
if (typeof suffix === 'string' && b.endsWith(suffix)) { b = b.slice(0, -suffix.length) }
return b }
// php/filesystem/pathinfo (target function module) functionpathinfo(path, options) { // discuss at: https://locutus.io/php/pathinfo/ // original by: Nate // revised by: Kevin van Zonneveld (https://kvz.io) // improved by: Brett Zamir (https://brett-zamir.me) // improved by: Dmitry Gorelenkov // input by: Timo // note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559 // note 1: The way the bitwise arguments are handled allows for greater flexibility // note 1: & compatability. We might even standardize this // note 1: code and use a similar approach for // note 1: other bitwise PHP functions // note 1: Locutus tries very hard to stay away from a core.js // note 1: file with global dependencies, because we like // note 1: that you can just take a couple of functions and be on your way. // note 1: But by way we implemented this function, // note 1: if you want you can still declare the PATHINFO_* // note 1: yourself, and then you can use: // note 1: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION); // note 1: which makes it fully compliant with PHP syntax. // example 1: pathinfo('/www/htdocs/index.html', 1) // returns 1: '/www/htdocs' // example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME') // returns 2: 'index.html' // example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION') // returns 3: 'html' // example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME') // returns 4: 'index' // example 5: pathinfo('/www/htdocs/index.html', 2 | 4) // returns 5: {basename: 'index.html', extension: 'html'} // example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL') // returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'} // example 7: pathinfo('/www/htdocs/index.html') // returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
const tmpArr = {} let basenameValue = null let extensionValue = null let filenameValue = null
// If array contains only 1 element: return string const values = Object.values(tmpArr).filter((value) =>typeof value === 'string') if (values.length === 1) { return values[0] ?? '' }
// Return full-blown array return tmpArr }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.