Is there a widely accepted class for dealing with URLs in PHP?
是否有一个广泛接受的类来处理PHP中的URL?
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.
例如:获取/更改现有URL的部分(例如路径,方案等),从基本URL解析相对路径。有点像双向parse_url(),用一堆方便的函数封装。
Does something like this exist?
这样的事情存在吗?
3 个解决方案
#1
3
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".
你已经在PEAR上获得了Net_URL2包,它似乎已经取代了原来的Net_URL。我没有第一手经验,但我几乎总是把“PEAR”包裹放在“网站上的随机库”中。
#2
4
This URL.php class may be a good start (not sure it is 'widely' accepted though).
这个URL.php类可能是一个好的开始(不确定它是'广泛'接受的)。
URL class intended for http and https schemes
用于http和https方案的URL类
This class allows you store absolute or relative URLs and access it's various parts (scheme, host, port, part, query, fragment).
此类允许您存储绝对或相对URL并访问它的各个部分(方案,主机,端口,部件,查询,片段)。
It will also accept and attempt to resolve a relative URL against an absolute URL already stored.
它还将接受并尝试针对已存储的绝对URL解析相对URL。
Note: this URL class is based on the HTTP scheme.
注意:此URL类基于HTTP方案。
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
#1
3
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".
你已经在PEAR上获得了Net_URL2包,它似乎已经取代了原来的Net_URL。我没有第一手经验,但我几乎总是把“PEAR”包裹放在“网站上的随机库”中。
#2
4
This URL.php class may be a good start (not sure it is 'widely' accepted though).
这个URL.php类可能是一个好的开始(不确定它是'广泛'接受的)。
URL class intended for http and https schemes
用于http和https方案的URL类
This class allows you store absolute or relative URLs and access it's various parts (scheme, host, port, part, query, fragment).
此类允许您存储绝对或相对URL并访问它的各个部分(方案,主机,端口,部件,查询,片段)。
It will also accept and attempt to resolve a relative URL against an absolute URL already stored.
它还将接受并尝试针对已存储的绝对URL解析相对URL。
Note: this URL class is based on the HTTP scheme.
注意:此URL类基于HTTP方案。
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