skix
skix

Reputation:

Is there a good pre-existing class for dealing with URLs in PHP?

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?

Upvotes: 3

Views: 219

Answers (3)

Alana Storm
Alana Storm

Reputation: 166086

You've got the Net_URL2 package over at PEAR, which appears to have replaced the 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".

Upvotes: 3

troelskn
troelskn

Reputation: 117517

Zend_Uri is a good candidate.

Upvotes: -1

VonC
VonC

Reputation: 1324977

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

Upvotes: 4

Related Questions