Extract file extension: Difference between revisions

initial submission of php 7.x solution to this problem
(initial submission of php 7.x solution to this problem)
Line 1,844:
</pre>
The builtin get_file_extension() could also be used, however that routine differs from the task description in that "libglfw.so.3.1" => "so", and all results are lowercase even if the input is not.
 
=={{header|PHP}}==
<lang php>
$tests = [
['input'=>'http://example.com/download.tar.gz', 'expect'=>'.gz'],
['input'=>'CharacterModel.3DS', 'expect'=>'.3DS'],
['input'=>'.desktop', 'expect'=>'.desktop'],
['input'=>'document', 'expect'=>''],
['input'=>'document.txt_backup', 'expect'=>''],
['input'=>'/etc/pam.d/login', 'expect'=>'']
];
 
foreach ($tests as $key=>$test) {
$ext = pathinfo($test['input'], PATHINFO_EXTENSION);
// in php, pathinfo allows for an underscore in the file extension
// the following if statement only allows for A-z0-9 in the extension
if (ctype_alnum($ext)) {
// pathinfo returns the extension without the preceeding '.' so adding it back on
$tests[$key]['actual'] = '.'.$ext;
} else {
$tests[$key]['actual'] = '';
}
}
foreach ($tests as $test) {
printf("%35s -> %s \n", $test['input'],$test['actual']);
}</lang>
{{out}}
<pre>
http://example.com/download.tar.gz -> .gz
CharacterModel.3DS -> .3DS
.desktop -> .desktop
document ->
document.txt_backup ->
/etc/pam.d/login ->
</pre>
 
=={{header|PicoLisp}}==
Line 1,871 ⟶ 1,906:
NIL
</pre>
 
 
=={{header|PowerShell}}==
Anonymous user