在获取当前URL中,碰到几个好用的函数:parse_url用来做信息抓取的处理,并返回关联数组。mixed parse_url ( string $url [, int $component = -1 ] ),component 指定 PHP_URL_SCHEME、 PHP_URL_HOST、 PHP_URL_PORT、 PHP_URL_USER、 PHP_URL_PASS、 PHP_URL_PATH、 PHP_URL_QUERY 或 PHP_URL_FRAGMENT 的其中一个来获取 URL 中指定的部分的 string。 (除了指定为 PHP_URL_PORT 后,将返回一个 integer 的值)。我测试了一下parse_url(),返回数组有四个键值:[scheeme]=http [host]=>www.xxx.com [path]=>/../.../ [query]=>a=1&b=2&... 其中[path]中是文件的路径 [query]是后面跟的参数。还有键值[fragment]在散列符号#之后,
第二个函数parse_str() void parse_str ( string $str [, array &$arr ] ) 传入的字符串转为变量 str输的字符串 arr变量以数组的形式存入其中,以下是我测试:$url = "http://www.junzihaoqiu.cn/xx/default.php?b=1&u=5&p=0&c=0";
$par = parse_url($url);
if (isset($par['query'])) {
parse_str($par['query'],$query);
print_r($query);//输出Array ( [b] => 1 [u] => 5 [p] => 0 [c] => 0 )
}
第三个函数http_build_query()不用说明也能知道它的作用了,
<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
以上会输出
foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&baz=boom&cow=milk&php=hypertext+processor