PHP中url相关函数
1,string urlencode(string $str)/string urldecode(string $str)
<?php $data = array( 'foo' => 'bar' , 'baz' => 'boom' , 'cow' => 'milk' , 'php' => 'hypertext processor' ); echo http_build_query ( $data ) . "<br/>" ; echo http_build_query ( $data , '' , '&' ); ?>
输出:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
3,parse_url()函数,解析一个 URL,将URL中出现各种部分存入关联数组返回。
mixed parse_url(string $url,int $component),$url为待解析url字符串,$component指定返回url中某一部分。如果$component没有指定,则以数组形式全部返回;如果$component已指定,则返回此部分字符串。
如:
<?php $url = 'http://username:password@hostname:9090/path?arg=value#anchor'; var_dump(parse_url($url)); var_dump(parse_url($url, PHP_URL_SCHEME)); var_dump(parse_url($url, PHP_URL_USER)); var_dump(parse_url($url, PHP_URL_PASS)); var_dump(parse_url($url, PHP_URL_HOST)); var_dump(parse_url($url, PHP_URL_PORT)); var_dump(parse_url($url, PHP_URL_PATH)); var_dump(parse_url($url, PHP_URL_QUERY)); var_dump(parse_url($url, PHP_URL_FRAGMENT));
输出:
array(8) { ["scheme"]=> string(4) "http" ["host"]=> string(8) "hostname" ["port"]=> int(9090) ["user"]=> string(8) "username" ["pass"]=> string(8) "password" ["path"]=> string(5) "/path" ["query"]=> string(9) "arg=value" ["fragment"]=> string(6) "anchor" } string(4) "http" string(8) "username" string(8) "password" string(8) "hostname" int(9090) string(5) "/path" string(9) "arg=value" string(6) "anchor"
4,