note 18384 added to function.pathinfo

From: Date: Mon, 21 Jan 2002 02:29:33 +0000
Subject: note 18384 added to function.pathinfo
Groups: php.notes 
Request: Send a blank email to php-notes+get-25599@lists.php.net to get a copy of this message
this function isn't very well tested here's my working version (well on FreeBSD with 4.1.1 in mod_php4 on Apache anyway) ==================== function crackpath($path) { $sep = strpos($path, "/") > -1 ? "/" : "\\"; $parts = explode($sep, $path); $file = $parts[sizeof($parts)-1]; $pi = pathinfo($file); $dir = implode($sep, array_slice($parts, 0, -1)); (!$dir && $file) && $dir = "."; $lenex = strlen($pi["extension"]); if($lenex) $pi["basename"] = substr($file, 0, - ($lenex+1)); else $file && $pi["basename"] = $file; $pi["basename"]; $dir && $pi["dirname"] = $dir ? $dir : "."; return $pi; } ======================== print_r(pathinfo("/foo/bar.bar/baz")); /* wrong answer Array ( [dirname] => /foo/bar.bar [basename] => baz [extension] => bar/baz ) */ print_r(crackpath("/foo/bar.bar/baz")); /* right answer Array ( [dirname] => /foo/bar.bar [basename] => baz ) */ print_r(pathinfo("/foo/bar.bar/baz/")); /* wrong again Array ( [dirname] => /foo/bar.bar [basename] => baz [extension] => bar/baz/ ) */ print_r(crackpath("/foo/bar.bar/baz/")); /* right Array ( [dirname] => /foo/bar.bar/baz [basename] => ) */ print_r(crackpath("\\foo\\bar.bar\\baz")); /* ooh i do windows too Array ( [dirname] => \foo\bar.bar [basename] => baz ) */ print_r(pathinfo("")); /* bah don't use array_key_exists to check for filename ! Array ( [basename] => ) */ print_r(crackpath("")); /* better copy this "broken" functionality Array ( [basename] => ) */ print_r(pathinfo("/foo/bar.")); /* interesting Array ( [basename] => bar. [dirname] => foo ) */ print_r(crackpath("")); /* better copy this too Array ( [basename] => bar. [dirname] => foo ) */ -- http://www.php.net/manual/en/function.pathinfo.php http://master.php.net/manage/user-notes.php?action=edit+18384 http://master.php.net/manage/user-notes.php?action=delete+18384 http://master.php.net/manage/user-notes.php?action=reject+18384

« previous php.notes (#25599) next »