Class URL

Description

URL class intended for http and https schemes

This class allows you store absolute or relative URLs and access it's various parts (scheme, host, port, part, query, fragment).

It will also accept and attempt to resolve a relative URL against an absolute URL already stored.

Note: this URL class is based on the HTTP scheme.

Example:


1 $url =& new URL('http://www.domain.com/path/file.php?query=blah');
2 echo $url->get_scheme(),"\n"; // http
3 echo $url->get_host(),"\n"; // www.domain.com
4 echo $url->get_path(),"\n"; // /path/file.php
5 echo $url->get_query(),"\n"; // query=blah
6 // Setting a relative URL against our existing URL
7 $url->set_relative('../great.php');
8 echo $url->as_string();

See test_URL.php file for examples of how relative URLs are handled.

CHANGES:

  • 0.3 (15-Jul-2003) - equal_to() method added.
  • 0.2 (30-Dec-2002) - Class name changed from Url to URL. - Added $use_default param to the get_port() method. - Added clone() method. - Added as_string() method, which is what I should've had instead of get() - Added parameter to as_string() method: $include_fragment (default: true), passing false to as_string() will omit the fragment and crosshatch ('#') from the URL
  • 0.1 - Initial release
TODO:
  • modify set_relative() to accept URL objects as well as strings

Located in Program_Root/URL.php (line 69)


	
			
Method Summary
void URL ([string $url = null])
string as_string ([mixed $fragment = URL_OPTION_WITH_FRAG], int $option)
object &clone ()
bool equal_to (mixed $url)
string get ()
string get_fragment ()
string get_host ()
string get_pass ()
string get_path ()
string get_path_query ()
int get_port ([bool $use_default = false])
string get_query ()
string get_scheme ()
string get_user ()
bool is_absolute ()
bool is_relative ()
void set (string $url)
void set_relative (string $url)
Methods
Constructor URL (line 141)

Constructor

Optional parameter accepts a URL string

void URL ([string $url = null])
  • string $url
as_string (line 340)

As string

Returns the full URL (excluding any user info). Optional parameter allows you to specify whether you want the fragment (if available) to be included (default behaviour) in the resulting URL, or omitted. Passing false to as_string() will omit the fragment and crosshatch ('#') from the returned result.

  • since:

    0.2

string as_string ([mixed $fragment = URL_OPTION_WITH_FRAG], int $option)
  • int $option:

    URL_OPTION_WITH_FRAG (default) or URL_OPTION_NO_FRAG

clone (line 173)

Clone

Example:


1 $url_copy =& $url->clone();

object &clone ()
equal_to (line 195)

Equal to

Returns true if $url is equal to current URL object. I'm hoping this method reflects RFC 2616 Section 3.2.3

Note: this method will not compare the following:

  • user info (username and password)
  • fragment (#fragment)

bool equal_to (mixed $url)
  • mixed $url:

    string URL or instance of URL class

get (line 323)

Get URL

Returns the full URL (excluding any user info).

  • see: URL::as_string()
  • deprecated:

    deprecated since version 0.2, use as_string() method instead.

string get ()
get_fragment (line 501)

Get fragment

Returns everything after the "#", or false if no fragment was specified

string get_fragment ()
get_host (line 433)

Get host

Returns the hostname/ip, or false if no hostname/ip was specified

string get_host ()
get_pass (line 422)

Get password

Returns the password, or false if no password was specified.

string get_pass ()
get_path (line 467)

Get path

string get_path ()
get_path_query (line 490)

Get path and query

Returns the path and (if available) the query

  • since:

    0.2

string get_path_query ()
get_port (line 450)

Get port

Returns the port number, or false if no port was specified.

If you pass true to get_port(), a default port will be returned if no port is found. This is based on checking if the URL is using the HTTP scheme (if so, 80 will be returned), or HTTPS scheme (if so, 443 will be returned).

int get_port ([bool $use_default = false])
  • bool $use_default:

    (optional) default: false

get_query (line 478)

Get query

Returns everything after the "?", or false if no query was specified

string get_query ()
get_scheme (line 400)

Get scheme

Returns the scheme, or false if no scheme was specified.

string get_scheme ()
get_user (line 411)

Get username

Returns the username, or false if no username was specified.

string get_user ()
is_absolute (line 377)

Is absolute URL

Returns true if scheme was specified

bool is_absolute ()
is_relative (line 389)

Is relative URL

Opposite of is_absolute()

bool is_relative ()
set (line 156)

Set URL

Will overwrite all existing URL parts (see set_relative() to set a relative URL)

void set (string $url)
  • string $url
set_relative (line 250)

Set relative URL

Sets a URL as relative to the current URL (base). An absolute URL passed to this method will overwrite all existing URL parts stored. I'm hoping this method reflects RFC 2396 Section 5.2

void set_relative (string $url)
  • string $url

Documention generated on Wed, 16 Jul 2003 01:04:00 +0100 by phpDocumentor 1.2.0