Stripping URL information into variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

SFADuncan
Forum Newbie
Posts: 23
Joined: Mon Aug 11, 2003 5:06 pm
Location: uk

Stripping URL information into variables

Post by SFADuncan »

How do I take the URL of a current page and strip it into different parts

I'm trying to isolate the directory path of the url of a current page

eg
http://www.domain.co.uk/directory

I wish to isolate "directory" and make it equal a variable.

Simon
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]parse_url[/php_man]()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phooey! :P
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

Beat yah! by at least.. ohhh.. 40 seconds ;)
SFADuncan
Forum Newbie
Posts: 23
Joined: Mon Aug 11, 2003 5:06 pm
Location: uk

Post by SFADuncan »

Hi thanks for these links... I had seen them... but was put off by the very first comment in the User Contributed Notes

"Its all very well being able to split and reconstruct a url. BUT how on earth do you get the FULL url of the current page."

since it is exactly this which I am trying to do.... but I will ignore this comment and attempt to use what is listed on this page
SFADuncan
Forum Newbie
Posts: 23
Joined: Mon Aug 11, 2003 5:06 pm
Location: uk

Post by SFADuncan »

No luck... all the scripts I see involve typing in the URL (within the script) ... I want to use the URL of the current page
SFADuncan
Forum Newbie
Posts: 23
Joined: Mon Aug 11, 2003 5:06 pm
Location: uk

Post by SFADuncan »

This script almost works:

<?php
echo "Current directory is " . getcwd();
?>

Except that I get:

Current directory is /home/virtual/site214/fst/var/www/html/test

when all I want is:

Current directory is test

Any ideas?

Simon
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Ugly but quick:

Code: Select all

$url = getcwd();
$array = explode ("/", $url);
$arrayB = array_reverse($array);

$folder = $arrayB[0];
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Okay, how is the cwd the same thing as trying to get the URL and parsing that? I am sorry, but if you are going to ask the question, ask the right question.

What are you trying to accomplish? Don't tell me what you think the solution is, tell me what you want.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Look at the $_SERVER variables. There's one in there about the script_uri.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

jason wrote:I am sorry, but if you are going to ask the question, ask the right question.
To be fair, he did ask the right question.
He said:
SFADuncan wrote:I'm trying to isolate the directory path of the url of a current page
getcwd() is one way of achieving this.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

You could try...

Code: Select all

<?php

$pwd = pathinfo(getcwd());
$cwd = $pwd['basename'];

echo $cwd;
?>
Although this will probably give undesireable results if run in your www root.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

What the hell? Why mess with cwd? To get the "full" URL of the current page on your site, just combine two values:

$_SERVER['HTTP_HOST'] and either $_SERVER['PHP_SELF'] or $_SERVER['REQUEST_URI'] if you need all the query string values.

Merge the two and kick the result to parse_url.
Post Reply