教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php url函数用法举例

php url函数用法举例

发布时间:2017-12-01   编辑:jiaochengji.com
本文详细介绍了PHP处理url字符串编码、解码与解析的函数及实例,感兴趣的朋友做个参考。

本节内容:
php 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
$phpha1 = get_headers('http://www.jbxue.com');
$phpha2 = get_headers('http://www.jbxue.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://www.jbxue.com/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] => http://www.jbxue.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
//天涯技术开发博客 http://www.jbxue.com
$phpha = get_meta_tags('http://www.jbxue.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, 'jb51_');
?>
 

这个函数目前我用的最多的地方就是做各种API时,组合请求的url,非常的方便。
另外可以看到,对于数组内数字索引的成员,还可以指定前缀。
parse_url — 解析 URL,返回其组成部分
本函数解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分。本函数不是用来验证给定 URL 的合法性的,只是将其分解为下面列出的部分。不完整的 URL 也被接受,parse_url() 会尝试尽量正确地将其解析。

例子:
 

复制代码 代码示例:
<?php
$url = 'http://tianya:jbxue.com@jbxue.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
$url = 'http://www.jbxue.com tianya';
echo urlencode($url);
echo '<br />';
echo rawurlencode($url);
echo '<br />';
echo urldecode($url);
echo '<br />';
echo rawurldecode($url);
?>
 

输出如下:
http%3A%2F%2Fwww.jbxue.com+tianya
http%3A%2F%2Fwww.jbxue.com%20tianya
可以看到,urlencode与rawurlencode的区别在于:
urlencode() 会把空格编码为加号(+),rawurlencode() 则把空格编码为 %20
urldecode()和rawurldecode() 则为对应的解码函数。

>>> 您可能感兴趣的文章:
php unicode解码工具(unicode编码转换器)
PHP 数组字符集编码转换的函数
php url编码与解码(加密/解密)
php检测字符串编码是否为utf8编码的5种方法
php硬编码登录表单一例
php判断字符串编码是否为utf8的函数举例
php获取字符串编码的函数mb_detect_encoding
php中UNICODE 编码与解码的二个例子
php获取字符串的编码格式的函数
PHP字符串编码问题分析
php iconv 函数转换gb2312编码时遇到的问题
php iconv字符串编码转换(GBK到UTF8字符集)的例子
php 自动检测内容编码并转换的代码
php实现utf-8和GB2312编码相互转换
php 中的url编码处理
php编码转换函数(自动转换字符集支持数组转换)
php编码转换函数mb_convert_encoding与iconv使用说明
php改变编码的函数iconv

您可能感兴趣的文章:
php file_get_contents函数抓取页面信息的代码
php页面跳转函数 页面重定向
php url函数用法举例
PHP MYSQL开发 视频教程
php 获取网站地址的函数代码
php header()函数的简单例子
php生成csv文件header设置参考
PHP 获取域名的几种方法
php 从url中获取域名的实例代码
PHP正则匹配获取URL中域名的代码

关键词: 编码  编码转换   
[关闭]
~ ~