--- warning: This file is auto generated by `yarn web:inject`, do not edit by hand examples: - basename('/www/site/home.htm', '.htm') - basename('ecra.php?p=1') - basename('/some/path/') - basename('/some/path_ext.ext/','.ext') returns: - '''home''' - '''ecra.php?p=1''' - '''path''' - '''path_ext''' dependencies: [] authors: original by: - Kevin van Zonneveld (https://kvz.io) improved by: - Ash Searle (https://hexmen.com/blog/) - Lincoln Ramsay - djmix - Dmitry Gorelenkov notes: [] parityVerified: - PHP 8.3 isTypescript: true type: function layout: function title: PHP's basename in TypeScript description: >- Here's what our current TypeScript equivalent to PHP's basename looks like. function: basename category: filesystem language: php permalink: php/filesystem/basename/ alias: - /functions/php/basename/ - /functions/filesystem/basename/ - /php/basename/ - /functions/basename/ ---
{% codeblock lang:typescript %}export function basename(path: string, suffix?: string): string { // 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 } {% endcodeblock %}