note 110137 deleted from function.filetype by aharvey
| From: | aharvey@php.net | Date: | Mon, 24 Sep 2012 01:28:32 +0000 |
| Subject: | note 110137 deleted from function.filetype by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190981@lists.php.net to get a copy of this message | ||
Note Submitter: brian dot burleigh at bigpond dot com
----
Use array to decompose any.file_name.file_type on the last ".". Haven't considered
execution efficiency... (and I'm not up to colouring in!)
<?php
// Testbed for Filename split function subroutine returns .suffix from a filename
// Filename input from CLI command parameter
var_dump($argv);
$pfile_name = $argv[1];
// Print Results
echo "Split: "
.filenamesplit($pfile_name)[0]."|||".filenamesplit($pfile_name)[1].PHP_EOL;
// ------------------------------------
function filenamesplit($file_name)
{
// Parse filename by decompostion into array
$bitearr =explode(".",$file_name); // break into bites on the dots
$bite_els =count($bitearr); // Number of bites
$prefarr = array_slice($bitearr,0,$bite_els-1); //drop last element
$prefix_name = implode(".",$prefarr); // just reassemble without last bite
$suffix_name = ".".end($bitearr); //Dot plus last bite
return array($prefix_name, $suffix_name) ;
}
// Sample Input: string(13) ".my.file.type"
// Split: .my.file|||.type
?>