tags:

views:

128

answers:

3

Is there a widely accepted class for dealing with URLs in PHP?

Things like: getting/changing parts of an existing URL (e.g. path, scheme, etc), resolving relative paths from a base URL. Kind of like a two-way parse_url(), encapsulated with a bunch of handy functions.

Does something like this exist?

+4  A: 

This URL.php class may be a good start (not sure it is 'widely' accepted though).

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:

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

You've got the Net_URL2 package over at PEAR, which appears to have replaced the original original Net_URL. I have no first hand experience with it, but I'll almost always take a PEAR package over "random library found on website".

Alan Storm
A: 

Zend_Uri is a good candidate.

troelskn