Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2011, 08:18 PM   PM User | #1
coding_begins
Regular Coder

 
Join Date: Aug 2011
Posts: 134
Thanks: 20
Thanked 0 Times in 0 Posts
coding_begins is an unknown quantity at this point
extract part of a string

Hi i need help with some string manipulation and extraction..
http://www.test.com/forums/attachmen...d-dsc00005.jpg

I need to extract an id from the above path dynamically.
I need to extract the digits before the alphabet d after the second last path f39/
In the above case the number extracted would be 745.
Can someone please help me out with it?

the thing is f39 is not constant..it may vary for different websites..i need to extract the number after the last slash / Can this be done?
coding_begins is offline   Reply With Quote
Old 11-23-2011, 08:24 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is the 'd' constant?
Fou-Lu is offline   Reply With Quote
Old 11-23-2011, 08:24 PM   PM User | #3
coding_begins
Regular Coder

 
Join Date: Aug 2011
Posts: 134
Thanks: 20
Thanked 0 Times in 0 Posts
coding_begins is an unknown quantity at this point
yes d would be constant always..
coding_begins is offline   Reply With Quote
Old 11-23-2011, 08:39 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Then you can simply do as so:
PHP Code:
$str 'http://www.test.com/forums/attachment/f39/6066d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg';
$filename pathinfo($strPATHINFO_FILENAME);
$part substr($filename0strpos($filename'd') - strlen($filename));
print 
$part
regex is another alternative.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
coding_begins (11-23-2011)
Old 11-23-2011, 09:19 PM   PM User | #5
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Yet another alternative, just for the heck of it:
PHP Code:
$str 'http://www.test.com/forums/attachment/f39/745d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg';

list( 
$num ) = explode'd'basename$str ) );

echo 
$num// 745 
__________________
ZCE
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:42 PM.


Advertisement
Log in to turn off these ads.