1.http_build_query
string http_build_query ( mixed $query_data
[, string $numeric_prefix
[, string $arg_separator
[, int $enc_type
= PHP_QUERY_RFC1738
]]] )
使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。
参数:
query_data
可以是数组或包含属性的对象。
一个query_data数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。
如果query_data是一个对象,只有public属性才会加入结果。
numeric_prefix
如果在基础数组中使用了数字下标同时给出了该参数,此参数值将会作为基础数组中的数字下标元素的前缀。
这是为了让 PHP 或其它 CGI 程序在稍后对数据进行解码时获取合法的变量名。
arg_separator
除非指定并使用了这个参数,否则会用 arg_separator.output来分隔参数(php.ini中有此参数,默认是”&”)。
enc_type
默认使用 PHP_QUERY_RFC1738。
如果 enc_type 是 PHP_QUERY_RFC1738,则编码将会以 » RFC 1738 标准和 application/x-www-form-urlencoded 媒体类型进行编码,空格会被编码成加号(+)。
如果 enc_type 是 PHP_QUERY_RFC3986,将根据 » RFC 3986 编码,空格会被百分号编码(%20)。
例子1:只使用query_data参数
<?php$data = array( ???‘name‘ => ‘fdipzone‘, ???‘gender‘ => ‘male‘, ???‘profession‘ => ‘programmer‘, ???‘explain‘ => ‘a new programmer‘);echo http_build_query($data);?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
输出: name=fdipzone&gender=male&profession=programmer&explain=a+new+programmer
例子2: query_data使用一维下标数组,指定
numeric_prefix=info_,arg_separator=#,enc_type=PHP_QUERY_RFC3986
<?php$data = array(‘fdipzone‘,‘male‘,‘programmer‘,‘a new programmer‘);echo http_build_query($data, ‘info_‘, ‘#‘, PHP_QUERY_RFC3986);?>
- 1
- 2
- 3
- 4
输出:
info_0=fdipzone#info_1=male#info_2=programmer#info_3=a%20new%20programmer
2.parse_url
parse_url 解析url,返回其组成部分
mixed parse_url ( string $url [, int $component = -1 ] )
- 1
参数:
url
要解析的url,无效字符将使用_来替换
component
PHP_URL_PATH、 PHP_URL_QUERY 或 PHP_URL_FRAGMENT 的其中一个来获取 URL 中指定的部分的 string。 (除了指定为 PHP_URL_PORT 后,将返回一个 integer 的值)。
返回值:
对严重不合格的 URL,parse_url() 可能会返回 FALSE。
返回的数据一般包含以下几种
scheme(如http),host,port,user,pass,path,query(在问号?之后),fragment(在散列符号#之后)
例子:
<?php$url = ‘http://fdipzone:123456@www.fdipzone.com:80/test/index.php?id=1#tag‘;print_r(parse_url($url));echo parse_url($url, PHP_URL_SCHEME).PHP_EOL;echo parse_url($url, PHP_URL_HOST).PHP_EOL;echo parse_url($url, PHP_URL_PORT).PHP_EOL;echo parse_url($url, PHP_URL_USER).PHP_EOL;echo parse_url($url, PHP_URL_PASS).PHP_EOL;echo parse_url($url, PHP_URL_PATH).PHP_EOL;echo parse_url($url, PHP_URL_QUERY).PHP_EOL;echo parse_url($url, PHP_URL_FRAGMENT).PHP_EOL;?>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
输出:
Array( ???[scheme] => http ???[host] => www.fdipzone.com ???[port] => 80 ???[user] => fdipzone ???[pass] => 123456 ???[path] => /test/index.php ???[query] => id=1 ???[fragment] => tag)httpwww.fdipzone.com80fdipzone123456/test/index.phpid=1tag
php使用http_build_query,parse_url,parse_str创建与解析url详解
原文地址:http://www.cnblogs.com/imnzq/p/7479634.html