TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-17-2009, 08:51 AM   #1 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default Splitting Filenames

I have a directory that will hold word docs of meeting minutes. The word doc titles will be "Minutes 1-21-2009.doc". I would basically I want the names to be "Minutes - 1-21-2009". Right now my code looks like:
PHP Code:
$min_dir blah;
if(
is_dir($min_dir)){
if(
$min opendir($min_dir)){
while((
$file readdir($min)) !== false){
if (
$file != "." && $file != "..") {
list(
$fliemin,$fileextra) = explode(' ',$file);
list(
$filedate,$fileext) = explode('.',$fileextra);
"<li><a href=\"minutes/$file\">Minutes - $filedate</a></li>";
}
}
closedir($min);
}

This works fine, I just wanted to see if there was a cleaner way to make this a bit cleaner (the part in the inner most if())
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-17-2009, 10:48 AM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It's a totally different approach to your script, but I think the following is much cleaner.

PHP Code:
$path  'path/to/files';
$files glob($path.'/Minutes *.doc');
foreach (
$files as $file)
{
    
// Minutes 1-2-2003.doc
    
$basename basename($file);
    
// Minutes 1-2-2003
    
$no_ext   basename($basename'.doc');
    
// 1-2-2003
    
$date     substr($no_ext8);
    
printf('<li><a href="minutes/%s">Minutes - %s</a></li>'$basename$date);

Salathe is offline  
Reply With Quote
Old 06-17-2009, 05:53 PM   #3 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

...wow that is much cleaner. A few questions. I see you defined the extension explicitly, is there a way to catch if it is .docx? If the person upgrades.
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-17-2009, 06:04 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

For the glob, look into GLOB_BRACE (on the glob manual page if you want to look for multiple discrete possibilities (such as .doc and .docx). For the $no_ext line, you could call pathinfo($basename, PATHINFO_FILENAME) which will cut off the extension whatever it is.
Salathe is offline  
Reply With Quote
Old 06-17-2009, 06:22 PM   #5 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

I just looked, and you can use multiple astricks, so can't you just logically do:
PHP Code:
$files glob($path.'/Minutes *.doc*'); 
//or
$files glob($path.'/Minutes *'); 
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-17-2009, 11:40 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Sure, if you know the files in there will only ever be of a certain pattern which those match (for example, no ".doc.bak" files).
Salathe is offline  
Reply With Quote
Old 06-18-2009, 06:13 AM   #7 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

.doc.bak would be covered by the *.doc*. However it wouldn't be happy with stripping the ext.
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:21 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design