php中url函数介绍及使用示例
php  /  管理员 发布于 7年前   134
base64_encode ― 使用 MIME base64 对数据进行编码 base64_decode ― 对使用 MIME base64 编码的数据进行解码 get_headers ― 取得服务器响应一个 HTTP 请求所发送的所有标头 输出如下: get_meta_tags ― 从一个文件中提取所有的 meta 标签 content 属性,返回一个数组 输出如下: http_build_query ― 生成 URL-encode 之后的请求字符串 $url = array('c'=>'blog', 'a'=>'show', 'id'=>10, 'hello', 'world'); 这个函数目前我用的最多的地方就是做各种API时,组合请求的url,非常的方便。 parse_url ― 解析 URL,返回其组成部分 rawurlencode ― 按照 RFC 1738 对 URL 进行编码 输出如下: 可以看到,urlencode与rawurlencode的区别在于: 122 在 123 在 原梓番博客 在 博主 在 1111 在
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号
base64_encode() returns 使用 base64 对 data 进行编码。设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体。
Base64-encoded 数据要比原始数据多占用 33% 左右的空间。
$str = 'This is an encoded string';
// VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
echo base64_encode($str);
?>
base64_decode() 对 encoded_data 进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的。
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
// This is an encoded string
echo base64_decode($str);
?>
get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息。
如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
$phpha1 = get_headers('');
$phpha2 = get_headers('', 1);
print_r($phpha1);
print_r($phpha2);
?>
Array
(
[0] => HTTP/1.1 200 OK
[1] => Server: nginx/1.2.2
[2] => Date: Tue, 06 Nov 2012 10:17:59 GMT
[3] => Content-Type: text/html;
[4] => Connection: close
[5] => X-Powered-By: PHP/5.3.8
[6] => X-Pingback: xmlrpc.php
[7] => Via: 10.67.15.26
[8] => Set-Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000
)
Array
(
[0] => HTTP/1.1 200 OK
[Server] => nginx/1.2.2
[Date] => Tue, 06 Nov 2012 10:17:59 GMT
[Content-Type] => text/html; charset=UTF-8
[Connection] => close
[X-Powered-By] => PHP/5.3.8
[X-Pingback] => xmlrpc.php
[Via] => 10.67.15.21
[Set-Cookie] => saeut=124.127.138.35.1352197079055460; path=/; max-age=311040000
)
可以想象的到,某些网站可以方便的用此函数进行网站SEO信息的提取。
//天涯PHP博客
$phpha = get_meta_tags('');
print_r($phpha);
?>
Array
(
[keywords] => 天涯博客,PHP博客,PHP技术博客,PHP学习博客,PHP开发博客
[description] => 天涯PHP博客是以PHP为主的学习博客,记载PHPER的学习历程,关注互联网最新发展动态。
[generator] => WordPress 3.2.1
)
// c=blog&a=show&id=10&0=hello&1=world
echo http_build_query($url);
// c=blog&a=show&id=10&phpha_0=hello&phpha_1=world
echo http_build_query($url, 'jb51_');
?>
[/code]
另外可以看到,对于数组内数字索引的成员,还可以指定前缀。
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url() 会尝试尽量正确地将其解析。
$url = 'http://tianya:[email protected]/hello.php?id=10#nav';
print_r(parse_url($url));
?>
Array
(
[scheme] => http
[host] => phpha.com
[user] => tianya
[pass] => phphadotcom
[path] => /hello.php
[query] => id=10
[fragment] => nav
)
rawurldecode ― 对已编码的 URL 字符串进行解码
urlencode ― 编码 URL 字符串
urldecode ― 解码已编码的 URL 字符串
$url = ' tianya';
echo urlencode($url);
echo '
';
echo rawurlencode($url);
echo '
';
echo urldecode($url);
echo '
';
echo rawurldecode($url);
?>
http%3A%2F%2F+tianya
http%3A%2F%2F%20tianya
urlencode() 会把空格编码为加号(+),rawurlencode() 则把空格编码为 %20
urldecode()和rawurldecode() 则为对应的解码函数。您可能感兴趣的文章:
上一条:
php去除字符串换行符示例分享
下一条:
php中的filesystem文件系统函数介绍及使用示例