Sniffing out part of a URL?

Discussion in 'Web Design and Development' started by bigandy, Feb 15, 2007.

  1. bigandy macrumors G3

    bigandy

    Joined:
    Apr 30, 2004
    Location:
    Murka
    #1
    I'm needing a little script to take part of a URL from the 'current page'.

    What I'm looking for is the part after the ".com/", for example, I'd be looking for the "stuff" part of the URL below:
    Code:
    http://www.domain.com/[B]stuff[/B]/wherever/something.php

    I've got no idea what to do here. Can anyone help?


    It's all PHP...
     
  2. nomade macrumors member

    nomade

    Joined:
    Dec 2, 2006
    #2
    sniffing part of url

    use explode, specify "/" as your divider

    example:
    $url=explode('/',$line['url']);
    echo $url[2];

    Good luck:)
     
  3. bigandy thread starter macrumors G3

    bigandy

    Joined:
    Apr 30, 2004
    Location:
    Murka
    #3
    cheers. i'm just being a complete tool today, but i'm not quite understanding how to modify this completely...

    this is what i've done:

    Code:
    <?php
    
    $furl = $_SERVER['PHP_SELF'];
    				
    $url=explode('/',$line[$furl]);
    				
    echo $url[2];
    				
    ?>
    this is what it returns:

    Code:
    /scripts/header.php
    which is where the file currently is. that's all well and good, but it needs to *just* show this bit

    Code:
    scripts
    but i've completely drawn a doofus blank as to which bit i fiddle with to fix it. :eek:
     
  4. jsm4182 macrumors 6502

    Joined:
    Apr 3, 2006
    Location:
    Beacon, NY
    #4
    I think this is what you're looking for

    dirname($_SERVER['PHP_SELF'])
     
  5. bigandy thread starter macrumors G3

    bigandy

    Joined:
    Apr 30, 2004
    Location:
    Murka
    #5
    I tried this but I've hit another fence - I'm trying to do this in an include file - my header for the site. when this is in that file, every time it will return

    Code:
    /scripts
    The thing is, I need the directory that the file it's being called from is in, so I put this:

    Code:
    $dir = dirname($_SERVER['PHP_SELF']);
    at the top of the files calling the header via this code:

    Code:
    <?php include 'http://www.site.com/scripts/header.php'; ?>
    But it's not doing nowt.

    Will a string defined at the top of one file work properly in an included php file too, or just in the original file? :confused:
     
  6. stndn macrumors member

    Joined:
    Oct 22, 2006
    Location:
    earth
    #6
    Maybe it's just me, but can you elaborate mroe on what you are trying to do?

    Are you trying to:

    1.
    Get the base/root directory name of the script you called?
    Example: http://www.domain.com/stuff/wherever/something.php, get 'stuff' (per your first post).

    2.
    Get the directory name where the header is located at?
    Example: http://www.site.com/scripts/header.php, get 'stuff'

    3.
    Get the full directory name of the script you called?
    Example: http://www.domain.com/stuff/wherever/something.php, get 'stuff/wherever'.


    If it's the first or third, use $_SERVER['PHP_SELF']
    If it's the second, use __FILE__

    In actuality, the variable $_SERVER['PHP_SELF'] will return to you the full directory name and file you called.
    It will omit everything from http:// to the first /
    i.e.: It will NOT include http://www.domain.com.
    Instead, it will start from /stuff/blahblah....

    Now, let's see if I'm successful in posting PHP code here...

    PHP:
    <?php

    # Assume this is the full URL you want to work on.
    # If you're using $_SERVER['PHP_SELF'], simply replace $parsedURL['path'] with the content of $_SERVER['PHP_SELF']

    $URL 'http://www.domain.com/stuff/wherever/something.php';
    $parsedURL parse_url ($URL);

    # If using explode (), note that empty strings may occur due to the beginning '/'
    # As such, the first directory you want is in index 1

    $splitPath explode ('/'$parsedURL['path']);
    print_r ($splitPath);

    /* Result:
        Array
        (
            [0] => 
            [1] => stuff
            [2] => wherever
            [3] => something.php
        )
     */


    # If using preg_split (), you can specify to omit empty strings
    # However, this is supposedly a little slower than simple explode ()

    $splitPath preg_split ('/\//'$parsedURL['path'], 0PREG_SPLIT_NO_EMPTY);
    print_r ($splitPath);

    /* Result:
        Array
        (
            [0] => stuff
            [1] => wherever
            [2] => something.php
        )
    */

    ?>
     
  7. bigandy thread starter macrumors G3

    bigandy

    Joined:
    Apr 30, 2004
    Location:
    Murka
    #7
    yup, it was option 1. that's helped no end. thanks everyone :)
     

Share This Page