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
    New Coder
    Join Date
    Dec 2007
    Location
    UK
    Posts
    41
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Removing charachers from end of string

    Hi all

    I have this string:

    http://www.whatever.com/testing/index.php

    I want to remove whatever is AFTER the last slash. So in the example above it would remove index.php

    Thanks in advance!
    Remember if someone has answered your question then please use the 'thank user' button below their post.

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    PHP Code:
    $url='http://www.whatever.com/testing/index.php';

    $trunc_url=substr($url0strrpos($url'/')+1); 
    You can perform the substr() and strrpos() calls on two different lines for readability.

  • Users who have thanked arnyinc for this post:

    tonyyeb (01-25-2008)

  • #3
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Here's what you need to learn this:

    strrpos() - http://us3.php.net/strrpos

    substr() - http://us.php.net/substr

    Basically, find the position of the last / in that string with strrpos(), and then substr() up to that / , and you've got what you need. Play around with it to get it how you need. If you need further assistance, just let us know!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by tonyyeb View Post
    Hi all

    I have this string:

    http://www.whatever.com/testing/index.php

    I want to remove whatever is AFTER the last slash. So in the example above it would remove index.php

    Thanks in advance!
    check your php version if you use this:

    PHP Code:
    $url "http://www.whatever.com/testing/index.php";
    echo 
    parse_url($urlPHP_URL_PATH); 
    and read:

    http://www.php.net/manual/en/function.parse-url.php

    another way:

    PHP Code:
    $url "http://www.whatever.com/testing/index.php";
    $url preg_replace("/[^\/]*$/","",$url);
    echo 
    $url
    both not tested,

    best regards

  • #5
    New Coder
    Join Date
    Dec 2007
    Location
    UK
    Posts
    41
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by arnyinc View Post
    PHP Code:
    $url='http://www.whatever.com/testing/index.php';

    $trunc_url=substr($url0strrpos($url'/')+1); 
    You can perform the substr() and strrpos() calls on two different lines for readability.
    Thanks everyone. This post lead me to the solution. Thanks all!
    Remember if someone has answered your question then please use the 'thank user' button below their post.


  •  

    Posting Permissions

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