SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replace text between two points

    i have a url e.g.
    http://www.jamesbirtles.com/blog/

    i want to extract "blog/" from it but the url wont always be that one e.g. it might be https://example.com/ablog/blog1/ (here i would want to extract "ablog/blog1/"). it could be any url but i want to remove "http://" or "https://" through to the first "/"

    Any help would be appreciated.
    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    parse_url() is probably the most straightforward way.

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    if $string is your full URL...

    $string = substr($string,strpos($string,"/",8));

    As long as your URL isnt http:///, it'll be fine.

  4. #4
    SitePoint Enthusiast Mr.Alexander's Avatar
    Join Date
    Apr 2010
    Location
    Toronto, Canada
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Greetings Manu,

    You could come to a solution using several methods.

    PHP allows you to use a set of "magic" arrays and the one we are particularly interested in is $_SERVER.

    This array has a number of values that you might want to explorer further.

    Lets start with http://, we will use getenv('HTTPS') if we are using https:// then it will return 'on' otherwise it will return false.

    $base = ( getenv('HTTPS') == 'on' ) ? 'https://' : 'http://';

    now we can see what is the "domain" that we are using, so we can use $_SERVER['SERVER_NAME'] which will return 'www.domain.com' or what ever that we want.

    $base .= $_SERVER['SERVER_NAME'];

    Next we have several options to extract the part after http://www.domain.com/ so we will use str_replace

    $string_to_extract = str_replace($base,'',$url);

    Hopefully it helps and I will be more then glad to answer further.

    Kind Regards,

    Mr. Alexander

  5. #5
    SitePoint Enthusiast Mr.Alexander's Avatar
    Join Date
    Apr 2010
    Location
    Toronto, Canada
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Manu,

    If you could be a bit more specific as to where are you getting the url variable ?

    This way there might be a shortcut way of doing it as well, as i mentioned using the $_SERVER array.

    Looking forward to help you,

    Mr. Alexander

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait hang on, i have just confused myself as to why i created this thread!
    I just realised i just need to use this:
    PHP Code:
    $_SERVER["REQUEST_URI"
    I was already using this except like this
    PHP Code:
    $pageURL 'http';
     if (
    $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     
    $pageURL .= "://";
     if (
    $_SERVER["SERVER_PORT"] != "80") {
      
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
      
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     } 
    Wow i'm dumb


Bookmarks

Posting Permissions

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