Is there a good existing class for working with URLs in PHP?

Is there a generally accepted class for working with URLs in PHP?

Things like getting / modifying parts of an existing URL (e.g. path, scheme, etc.), resolving relative paths from the base URL. View as a two-way parse_url () , encapsulated by a bunch of convenient functions.

Is there something similar?

+3
source share
3 answers

You have the Net_URL2 package in PEAR, which seems to have replaced the original Net_URL . I have no first experience with it, but I almost always take the PEAR package on top of the "random library found on the website."

+3
source

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

URL class for http and https schemes

This class allows you to store absolute or relative URLs and get different parts to them (scheme, host, port, part, request, fragment).

It will also accept and try to resolve the relative URL with the absolute URL already stored.

Note. This URL class is based on an 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 
+4
source

Zend_Uri is a good candidate.

-one
source

All Articles