View Full Version : extract part of a string

11-23-2011, 09:18 PM
Hi i need help with some string manipulation and extraction..

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?

11-23-2011, 09:24 PM
Is the 'd' constant?

11-23-2011, 09:24 PM
yes d would be constant always..

11-23-2011, 09:39 PM
Then you can simply do as so:

$str = 'http://www.test.com/forums/attachment/f39/6066d1267084199-official-ar-m16-m4-picture-thread-dsc00005.jpg';
$filename = pathinfo($str, PATHINFO_FILENAME);
$part = substr($filename, 0, strpos($filename, 'd') - strlen($filename));
print $part;

regex is another alternative.

11-23-2011, 10:19 PM
Yet another alternative, just for the heck of it:

$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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum