Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Is the 'd' constant?

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    134
    Thanks
    20
    Thanked 0 Times in 0 Posts
    yes d would be constant always..

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    coding_begins (11-23-2011)

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •