note 88654 modified in function.pathinfo by danbrown
| From: | danbrown@php.net | Date: | Mon, 02 Feb 2009 16:51:57 +0000 |
| Subject: | note 88654 modified in function.pathinfo by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150111@lists.php.net to get a copy of this message | ||
Here is a simple function that gets the extension of a file. Simply using PATHINFO_EXTENSION will
yield incorrect results if the path contains a query string with dots in the parameter names (for
eg. &x.1=2&y.1=5), so this function eliminates the query string first and subsequently runs
PATHINFO_EXTENSION on the clean path/url.
<?php
function extension($path) {
$qpos = strpos($path, "?");
if ($qpos!==false) $path = substr($path, 0, $qpos);
$extension = pathinfo($path, PATHINFO_EXTENSION);
return $extension;
}
?>
--was--
Here is a simple function that gets the extension of a file. Simply using PATHINFO_EXTENSION will
yield incorrect results if the path contains a query string with dots in the parameter names (for
eg. &x.1=2&y.1=5), so this function eliminates the query string first and subsequently runs
PATHINFO_EXTENSION on the clean path/url.
function extension($path) {
$qpos = strpos($path, "?");
if ($qpos!==false) $path = substr($path, 0, $qpos);
$extension = pathinfo($path, PATHINFO_EXTENSION);
return $extension;
}
Any room for improvement?
http://php.net/manual/en/function.pathinfo.php