note 120300 added to function.pathinfo
| From: | smartdogattut.by | Date: | Thu, 08 Dec 2016 11:02:08 +0000 |
| Subject: | note 120300 added to function.pathinfo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-208279@lists.php.net to get a copy of this message | ||
Lightweight way to get extension for *nix systems
<?php
function get_extension($path)
{
$c = preg_match('#[^\/]+\.([^\.]*)$#uis', $path, $tmp);
return $c ? $tmp[1] : null;
}
?>
this will return NULL for dotfiles (hidden files)
Testing:
<?php
$test = array(
'/normal_dir/normal.foo',
'/double_ext.foo.bar',
'/.hidden/empty_ext.',
'/.hidden_dir/.hidden_file',
'/foo.bar/no_ext'
);
foreach($test as $path) {
var_dump(parse_extension($path));
}
?>
results:
string(3) "foo"
string(3) "bar"
string(0) ""
NULL
NULL
if you want to get all extensions (substring of file name after first dot) use another expression:
<?php
$c = preg_match('#[^\.|\/]+\.([^\/]*)$#uis', $path, $tmp);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 37.17.45.76
----
Manual Page -- http://php.net/manual/en/function.pathinfo.php
Edit -- https://master.php.net/note/edit/120300
Del: integrated -- https://master.php.net/note/delete/120300/integrated
Del: useless -- https://master.php.net/note/delete/120300/useless
Del: bad code -- https://master.php.net/note/delete/120300/bad+code
Del: spam -- https://master.php.net/note/delete/120300/spam
Del: non-english -- https://master.php.net/note/delete/120300/non-english
Del: in docs -- https://master.php.net/note/delete/120300/in+docs
Del: other reasons-- https://master.php.net/note/delete/120300
Reject -- https://master.php.net/note/reject/120300
Search -- https://master.php.net/manage/user-notes.php