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 08-26-2008, 08:57 PM   #1 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default Gah.. Regex?

Hi,

If i have a file named something (123).jpg, where 123 is an integer, is there a way where I can get the integer to use in the rest of my script?

I think its regex- not sure though.

Thanks for any help :)
Gareth is offline  
Reply With Quote
Old 08-27-2008, 12:04 AM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

There's a few ways.

basename and explode come to mind.

PHP Code:
$file '/path/to/file/filename.ext';
$file_name basename($file); // returns "filename"

$file 'filename.ext';
$file_arr explode('.'$file);
echo 
$file_arr[0]; // returns "filename" 
delayedinsanity is offline  
Reply With Quote
Old 08-27-2008, 12:05 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

php Code:
$file = '/path/to/file/filename.ext';
$file_name = pathinfo($file, PATHINFO_FILENAME); // returns "filename"
 

php Code:
$pFile = (object) pathinfo('/path/to/file/filename.ext');

printf("Directory: %s<br />\n", $pFile->dirname);
printf("Base: %s<br />\n", $pFile->basename);
printf("Extension: %s<br />\n", $pFile->extension);
printf("Filename: %s<br />\n", $pFile->filename);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 08-27-2008, 12:18 AM   #4 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Thanks, but the problem is I only need part of the filename, specifically the part between the () (but not including them)...
Gareth is offline  
Reply With Quote
Old 08-27-2008, 12:22 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

php Code:
$pFile = (object) pathinfo('/path/to/file/(123).jpg');
printf("Filename: %s", preg_replace('~[^\d]~i', '', $pFile->filename));
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 08-27-2008, 12:51 AM   #6 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

I love you wildhoney- could you explain or point me in the right direction the preg_replace you gave me? Its greek to me (although funnily enough i did actually study greek...)...!
Gareth is offline  
Reply With Quote
Old 08-27-2008, 12:54 AM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Oh, your file is named 'something(123).jpg' not '123.jpg'.

I misunderstood.
-m
delayedinsanity is offline  
Reply With Quote
Old 08-27-2008, 01:00 AM   #8 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Its my fault, i should have made myself clearer :)
Gareth is offline  
Reply With Quote
Old 08-27-2008, 01:49 AM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

All it means is anything that's not an integer (^\d -- ^ being not, and \d meaning digit) should be replaced with null.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 08-27-2008, 11:58 AM   #10 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

Ahh, too easy! Thanks.
Gareth 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 07:43 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