摘自PHP手册[6] – URL函数
来源:互联网 发布:c 串口接收数据 编辑:程序博客网 时间:2024/05/22 03:12
简介:处理 URL 字符串:编码、解码与解析。下面天涯进行详细的举例说明。
base64_encode — 使用 MIME base64 对数据进行编码
base64_encode() returns 使用 base64 对 data 进行编码。设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体。
Base64-encoded 数据要比原始数据多占用 33% 左右的空间。
- <?php
- $str = 'This is an encoded string';
- // VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==
- echo base64_encode($str);
- ?>
base64_decode — 对使用 MIME base64 编码的数据进行解码
base64_decode() 对 encoded_data 进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的。
- <?php
- $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
- // This is an encoded string
- echo base64_decode($str);
- ?>
get_headers — 取得服务器响应一个 HTTP 请求所发送的所有标头
get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。如果失败则返回 FALSE 并发出一条 E_WARNING 级别的错误信息。
如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。
- <?php
- //天涯PHP博客 http://blog.phpha.com
- $phpha1 = get_headers('http://blog.phpha.com');
- $phpha2 = get_headers('http://blog.phpha.com', 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; charset=UTF-8
- [4] => Connection: close
- [5] => X-Powered-By: PHP/5.3.8
- [6] => X-Pingback: http://blog.phpha.com/xmlrpc.php
- [7] => Via: 10.67.15.26
- [8] => Set-Cookie: saeut=124.127.138.35.1352197078737175; path=/; max-age=311040000
- )
- //天涯PHP博客 http://blog.phpha.com
- 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] => http://blog.phpha.com/xmlrpc.php
- [Via] => 10.67.15.21
- [Set-Cookie] => saeut=124.127.138.35.1352197079055460; path=/; max-age=311040000
- )
get_meta_tags — 从一个文件中提取所有的 meta 标签 content 属性,返回一个数组
【天涯注】可以想象的到,某些网站可以方便的用此函数进行网站SEO信息的提取。
- <?php
- //天涯PHP博客 http://blog.phpha.com
- $phpha = get_meta_tags('http://blog.phpha.com');
- print_r($phpha);
- ?>
- //输出如下:
- Array
- (
- [keywords] => 天涯博客,PHP博客,PHP技术博客,PHP学习博客,PHP开发博客
- [description] => 天涯PHP博客是以PHP为主的学习博客,记载PHPER的学习历程,关注互联网最新发展动态。
- [generator] => WordPress 3.2.1
- )
http_build_query — 生成 URL-encode 之后的请求字符串
- <?php
- $url = array('c'=>'blog', 'a'=>'show', 'id'=>10, 'hello', 'world');
- // 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, 'phpha_');
- ?>
【天涯注】这个函数目前我用的最多的地方就是做各种API时,组合请求的url,非常的方便。
另外可以看到,对于数组内数字索引的成员,还可以指定前缀。
parse_url — 解析 URL,返回其组成部分
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url() 会尝试尽量正确地将其解析。
- <?php
- $url = 'http://tianya:phphadotcom@phpha.com/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
- )
rawurlencode — 按照 RFC 1738 对 URL 进行编码
rawurldecode — 对已编码的 URL 字符串进行解码
urlencode — 编码 URL 字符串
urldecode — 解码已编码的 URL 字符串
- <?php
- //天涯PHP博客 http://blog.phpha.com
- $url = 'http://blog.phpha.com tianya';
- echo urlencode($url);
- echo '<br />';
- echo rawurlencode($url);
- echo '<br />';
- echo urldecode($url);
- echo '<br />';
- echo rawurldecode($url);
- ?>
- 输出如下:
- http%3A%2F%2Fblog.phpha.com+tianya
- http%3A%2F%2Fblog.phpha.com%20tianya
- http://blog.phpha.com tianya
- http://blog.phpha.com tianya
可以看到,urlencode与rawurlencode的区别在于:
urlencode() 会把空格编码为加号(+),rawurlencode() 则把空格编码为 %20
urldecode()和rawurldecode() 则为对应的解码函数。
标签: PHP url 手册 系统函数
Posted under: PHP,PHP手册
Tagged as: PHP,url, 手册, 系统函数
- 摘自PHP手册[6] – URL函数
- 摘自PHP手册[2] – 常用系统函数概览
- 摘自PHP手册[3] – Classes/Objects 类/对象函数
- 摘自PHP手册[5] – Date/Time 日期/时间函数
- 摘自PHP手册[7] – Variable变量函数
- 摘自PHP手册[8] – String字符串处理函数
- 摘自PHP手册[9] – Array数组函数
- PHP - Manual手册 - CLXIX. URL 函数 - 概述
- 摘自PHP手册[1] – 需要注意的基础知识
- 摘自PHP手册[4] – Function Handling Functions
- PHP - Manual手册 - 函数参考 - CURL, Client URL Library Functions客户端URL库函数 - 概述
- PHP常用函数手册
- PHP函数手册大全
- PHP手册-嵌套函数
- PHP手册-魔术函数
- php redis 函数手册
- Xhprof-PHP函数手册]
- 【摘自网上】Delphi 7.0常用函数速查手册
- 摘自PHP手册[3] – Classes/Objects 类/对象函数
- 《数学之美》读书笔记
- LeetCode Combinations
- 摘自PHP手册[4] – Function Handling Functions
- 摘自PHP手册[5] – Date/Time 日期/时间函数
- 摘自PHP手册[6] – URL函数
- 摘自PHP手册[7] – Variable变量函数
- JAVA--第十二周任务之1.编写一个应用程序,要求编写一个Panel的子类MyPanel,MyPanel中有一个文本框和一个按钮,要求MyPanel的实例作为其按钮的ActionEvent事件的监视
- 摘自PHP手册[8] – String字符串处理函数
- 摘自PHP手册[9] – Array数组函数
- 如何删除非当前用户下的JOB
- SQLite
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- 从P1到P7——我在淘宝这7年