note 65383 added to function.basename
| From: | grahamatgrahamreevesdotcom at osu1 dot php dot net | Date: | Sat, 29 Apr 2006 21:12:17 +0000 |
| Subject: | note 65383 added to function.basename | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110127@lists.php.net to get a copy of this message | ||
I wanted to quickly extract an extension from a filename or url, eg. domain.com/test.gif (the
examples are exactly the same if you just submit test.gif)
<?
// match last *.[a-z] at end of string
$Filename = 'domain.com/test.gif';
$regs = array();
$result = ereg( '.\\.([a-zA-Z0-9]+)$', $Filename, $regs );
// no match (therefore no dot or, not dot then alpha numerics), this covers cases like
domain.com/sub.dir/test.gif
if ( $result == 0 )
return FALSE;
// for /domain.com/test.gif
// regs[0] will have test.gif
// regs[1] will have gif
return $regs[1];
?>
Then I have files with "sub-extensions", for example domain.com/test.thumb.gif
So I may want to [test for and] extract the thumb part
<?
// match last *.[a-z].[a-z] at end of string, if only one extension (eg *.[a-z]) then ereg fails
$Filename = 'domain.com/test.thumb.gif';
$regs = array();
$result = ereg( '\\.([a-zA-Z0-9]+)\\.([a-zA-Z0-9]+)$', $Filename, $regs );
// no match so either no extension, or only 1 extension
if ( $result == 0 )
return FALSE;
// for domain.com/test.thumb.gif
// regs[0] will have test.thumb.gif
// regs[1] will have thumb
// regs[2] will have gif
return $regs[1];
?>
Hope this helps someone, saves splitting strings and managing resulting arrays :)
*note* for the php.net note submission my backslash dot's in my regular expressions
weren't showing up, so if the system changes at a later date, it's supposed to be
onebackslash dot ([a..... etc, not two backslashes
----
Server IP: 85.116.0.130
Probable Submitter: 87.114.0.168
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.basename.php
Edit -- http://master.php.net/note/edit/65383
Del: integrated -- http://master.php.net/note/delete/65383/integrated
Del: useless -- http://master.php.net/note/delete/65383/useless
Del: bad code -- http://master.php.net/note/delete/65383/bad+code
Del: spam -- http://master.php.net/note/delete/65383/spam
Del: non-english -- http://master.php.net/note/delete/65383/non-english
Del: in docs -- http://master.php.net/note/delete/65383/in+docs
Del: other reasons-- http://master.php.net/note/delete/65383
Reject -- http://master.php.net/note/reject/65383
Search -- http://master.php.net/manage/user-notes.php