note 44179 deleted from ref.array by tomsommer

From: Date: Fri, 16 Jul 2004 20:09:37 +0000
Subject: note 44179 deleted from ref.array by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73415@lists.php.net to get a copy of this message
Note Submitter: housni@sltnet.lk ---- fullFileName(string FILE_NAME[, mixed OPTIONAL_PADDING_CHARACTER]) Returns an associative array This function will accept either a file name only or the file name plus the relative path to it but it only accepts strings (since it's only built to manipulate strings). If anything but a string is passed into it as the file name, the function will return FALSE. The function will return an associative array; the key 'type' will hold the extension of the file and the key 'name' will hold the full name of the file (even if the file name has multiple periods in its name) The OPTIONAL_PADDING_CHARACTER will replace any periods in the file name. This parameter is optional as you can see, but by default, that parameter is set to be a blank space. NOTE: I used a for loop instead of a iterating with foreach because an additional if statement would be necessary (to know when to break the iteration so that the extension is not added as a part of the name). <?php function fullFileName($fileInfo, $paddingChar = ' ') { if(!is_string($fileInfo)) { return false; } $nameExplode = explode('.', end(explode('/', $fileInfo))); $name = ''; if(count($nameExplode) > 2) { for($loopCount = 0; $loopCount < count($nameExplode) - 1; $loopCount++) { if($loopCount == 0) { $padding = ''; } else { $padding = $paddingChar; } $name .= $padding . $nameExplode[$loopCount]; } } else { $name = $nameExplode[0]; } $fileType = end($nameExplode); return array('type' => $fileType, 'name' => $name); } $file = fullFileName('path/to/file/file.class.extender.php', '-'); echo 'Type: ' . $file['type'] . '<br />Name: ' . $file['name']; /* * * The above code will yield: * Type: php * Name: file-class-extender * */ ?> Here are a few examples: <?php $file = fullFileName('file.class.extender.php', '>'); echo 'Type: ' . $file['type'] . '<br />Name: ' . $file['name']; /* * * The above code will yield: * Type: php * Name: file>class>extender * */ $file = fullFileName('extender.php'); echo 'Type: ' . $file['type'] . '<br />Name: ' . $file['name']; /* * * The above code will yield: * Type: php * Name: extender * */ $file = fullFileName('file.class.extender.php'); echo 'Type: ' . $file['type'] . '<br />Name: ' . $file['name']; /* * * The above code will yield: * Type: php * Name: file class extender * */ ?> Housni Yakoob http://www.koobi-studio.com/

« previous php.notes (#73415) next »