{"id":824,"date":"2008-04-27T22:09:21","date_gmt":"2008-04-27T20:09:21","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=824"},"modified":"2008-04-28T00:39:44","modified_gmt":"2008-04-27T22:39:44","slug":"javascript-basename-and-dirname","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2008\/04\/javascript-basename-and-dirname\/","title":{"rendered":"Javascript basename() and dirname()"},"content":{"rendered":"<p>For my next stuff I needed the Javascript equivalents of PHP functions basename() and dirname(). Nothing genius:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">javascript<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"javascript\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">function basename(path) {<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; return path.replace(\/\\\\\/g,'\/').replace( \/.*\\\/\/, '' );<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">}<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">function dirname(path) {<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; return path.replace(\/\\\\\/g,'\/').replace(\/\\\/[^\\\/]*$\/, '');;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">}<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>It obviously supports paths like <em>\/home\/ozh\/stuff.php<\/em>, <em>http:\/\/site.com\/file.ext<\/em> or double backslashed Win32  paths such as <em>C:\\\\dir\\\\document.txt<\/em>.<\/p>\n<p><strong>Edit:<\/strong> Mister Ze in comments points out to <a href=\"http:\/\/kevin.vanzonneveld.net\/techblog\/article\/phpjs_licensing\/\">PHP.JS<\/a>, an ongoing project aiming to port PHP to Javascript. Some really neat stuff there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my next stuff I needed the Javascript equivalents of PHP functions basename() and dirname(). Nothing genius: &lt; View plain text &gt; javascript function basename(path) { &nbsp; &nbsp; return path.replace(\/\\\\\/g,&#39;\/&#39;).replace( \/.*\\\/\/, &quot; ); } &nbsp; function dirname(path) { &nbsp; &nbsp; return path.replace(\/\\\\\/g,&#39;\/&#39;).replace(\/\\\/[^\\\/]*$\/, &quot;);; } It obviously supports paths like \/home\/ozh\/stuff.php, http:\/\/site.com\/file.ext or double backslashed Win32 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,7,10],"class_list":["post-824","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-javascript","tag-php"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=824"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/824\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}