Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bigandy

macrumors G3
Original poster
Apr 30, 2004
8,852
7
Murka
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...
 
sniffing part of url

use explode, specify "/" as your divider

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

Good luck:)
 
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:
 
I think this is what you're looking for

dirname($_SERVER['PHP_SELF'])

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:
 
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'], 0, PREG_SPLIT_NO_EMPTY);
print_r ($splitPath);

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

?>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.