PHP中压缩函数gzencode、gzdeflate和gzcompress使用

 更新时间:2016年11月25日 16:46  点击:2216
在php中gzencode、gzdeflate和gzcompress三个函数是用来对页面进行压缩传输的,下面一起来与小编深入了解gzencode、gzdeflate和gzcompress函数与性能吧。

gzencode 默认使用ZLIB_ENCODING_GZIP编码,使用gzip压缩格式,实际上是使用defalte 算法压缩数据,然后加上文件头和adler32校验
gzdeflate 默认使用ZLIB_ENCODING_RAW编码方式,使用deflate数据压缩算法,实际上是先用 LZ77 压缩,然后用霍夫曼编码压缩
gzcompress ;默认使用ZLIB_ENCODING_DEFLATE编码,使用zlib压缩格式,实际上是用 deflate 压缩数据,然后加上 zlib 头和 CRC 校验
这三个函数的比较实质上是三种压缩方法:deflate, zlib, gzip的比较。
从性能的维度看:deflate 好于 gzip 好于 zlib
从文本文件默认压缩率压缩后体积的维度看:deflate 好于 zlib 好于 gzip

这三种算法中gzip 、zlib的作者都是Jean-Loup Gailly和 Mark Adler。
这两种算法以及图形格式png,使用的压缩算法却都是deflate算法。
deflate算法是同时使用了LZ77算法与哈夫曼编码(Huffman Coding)的一个无损数据压缩算法。
它最初是由Phil Katz为他的PKZIP归档工具第二版所定义的,后来定义在 RFC 1951规范中。

deflate算法的压缩与解压的实现过程可以在压缩库zlib上找到。

PHP的压缩实现依赖于zlib,zlib是一个提供了 deflate, zlib, gzip 压缩方法的函数库。
我们所使用的上面三个函数,将参数中的encoding转为相同,压缩率设置相同,则其最终调用的是同一个函数,效果和性能一样。

PHP的zlib实现是以扩展的方式存在于ext/zlib目录中。通过deflateInit2() + deflate() + deflateEnd()三个函数配合完成压缩功能,inflateInit2() + inflate() + inflateEnd()三个函数配合完成解压功能。压缩最终都是通过php_zlib_encode函数实现调用,除了输入的字符串,压缩率,结果的输出外,不同的入口函数调用参数不同的是其encoding。deflateInit2的第四个参数指定encoding,PHP定义了三个常量:

 #define PHP_ZLIB_ENCODING_RAW          -0xf      //deflate -15
#define PHP_ZLIB_ENCODING_GZIP          0x1f      //gzip 15 + 16
#define PHP_ZLIB_ENCODING_DEFLATE     0x0f      // zlib 15

三个函数在调用过程可以直接指定encoding使用其它的算法:

zlib:   ZLIB_ENCODING_DEFLATE
gzip: ZLIB_ENCODING_GZIP
deflate: ZLIB_ENCODING_RAW

此三个函数是三种算法的简单调用方式,以更好的命名展现。三个函数间可以通过指定相同的encoding达到相同的效果,并且PHP也提供zlib_encode函数作为通用的压缩函数。

gzcompress、gzencode、gzdeflate压缩比比较

下面我们通过一段程序,来比较一下这三个函数的压缩比。

 代码如下 复制代码

$string = "8aeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer8aaaaaaaaaaaaaaaaaaaaaaadasd456as
 d456asd456asd456asd456asd456asd456asd456asd456asd456asd456asd456fasdf45645645645645645
 6456456456456456456456456456456456456456456456456456456456456456456456456a56fs4s4s4s4s
 4s4s4s4s4s4s4s4s4s4dsdga133333333333333333333w8etw7q9999999999999999999a23s1dfffffffff
 fffffffffffffffa456ssssssssssssdv2sdddddddddddddddddddf";
 echo "字符串长度:";
 echo strlen($xx);
 echo "<br/>gzcompress压缩后长度 :";
 echo strlen(gzcompress($string,9));
 echo "<br/>gzencode压缩后长度:";
 echo strlen(gzencode($string,9));
 echo "<br/>gzdeflate压缩后长度:";
 echo strlen(gzdeflate($string,9));


运行结果是:
字符串长度:388
gzcompress压缩后长度 :80
gzencode压缩后长度 :92
gzdeflate压缩后长度 :74

由此可见,这三个函数按照压缩效果进行排序:gzdeflate、gzcompress、gzencode,gzdeflate压缩效果最好,gzcompress次之,gzencode最差。

分析:
查了下手册,gzcompress、gzencode、gzdeflate这三个函数,分别采用的是zlib、gzip、DEFLATE方式压缩格式。通过上面的运行结果,不知道是否可以这么认为,DEFLATE、zlib、gzip的压缩效果递减

strtotime函数的使用在时间日期处理上是非常的强大了,这里我们一起来看看strtotime函数内核与常用方法。

PHP strtotime函数将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳]

一,获取指定日期的unix时间戳 strtotime(”2009-1-22″) 示例如下:

echo strtotime(”2009-1-22“) 结果:1232553600
说明:返回2009年1月22日0点0分0秒时间戳

二,获取英文文本日期时间 示例如下:

便于比较,使用date将当时间戳与指定时间戳转换成系统时间

 代码如下 复制代码

(1)打印明天此时的时间戳strtotime(”+1 day“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”+1 day”)) 结果:2009-01-23 09:40:25

(2)打印昨天此时的时间戳strtotime(”-1 day“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”-1 day”)) 结果:2009-01-21 09:40:25

(3)打印下个星期此时的时间戳strtotime(”+1 week“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”+1 week”)) 结果:2009-01-29 09:40:25

(4)打印上个星期此时的时间戳strtotime(”-1 week“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”-1 week”)) 结果:2009-01-15 09:40:25

(5)打印指定下星期几的时间戳strtotime(”next Thursday“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”next Thursday”)) 结果:2009-01-29 00:00:00

(6)打印指定上星期几的时间戳strtotime(”last Thursday“)
当前时间:echo date(”Y-m-d H:i:s”,time()) 结果:2009-01-22 09:40:25
指定时间:echo date(”Y-m-d H:i:s”,strtotime(”last Thursday”)) 结果:2009-01-15 00:00:00

以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。

现在我们深入分析它的源码
源码位置:\ext\date\php_date.c

 

 代码如下 复制代码

/* {{{ proto int strtotime(string time [, int now ])
   Convert string representation of date and time to a timestamp */
PHP_FUNCTION(strtotime)
{
    char *times, *initial_ts;
    int   time_len, error1, error2;
    struct timelib_error_container *error;
    long  preset_ts = 0, ts;

    timelib_time *t, *now;
    timelib_tzinfo *tzi;

    tzi = get_timezone_info(TSRMLS_C);

    if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, “sl”, &times, &time_len, &preset_ts) != FAILURE) {
        /* We have an initial timestamp */
        now = timelib_time_ctor();

        initial_ts = emalloc(25);
        snprintf(initial_ts, 24, “@%ld UTC”, preset_ts);
        t = timelib_strtotime(initial_ts, strlen(initial_ts), NULL, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); /* we ignore the error here, as this should never fail */
        timelib_update_ts(t, tzi);
        now->tz_info = tzi;
        now->zone_type = TIMELIB_ZONETYPE_ID;
        timelib_unixtime2local(now, t->sse);
        timelib_time_dtor(t);
        efree(initial_ts);
    } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, “s|l”, &times, &time_len, &preset_ts) != FAILURE) {
        /* We have no initial timestamp */
        now = timelib_time_ctor();
        now->tz_info = tzi;
        now->zone_type = TIMELIB_ZONETYPE_ID;
        timelib_unixtime2local(now, (timelib_sll) time(NULL));
    } else {
        RETURN_FALSE;
    }

    if (!time_len) {
        timelib_time_dtor(now);   
        RETURN_FALSE;
    }

    t = timelib_strtotime(times, time_len, &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
    error1 = error->error_count;
    timelib_error_container_dtor(error);
    timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
    timelib_update_ts(t, tzi);
    ts = timelib_date_to_int(t, &error2);

    timelib_time_dtor(now);
    timelib_time_dtor(t);

    if (error1 || error2) {
        RETURN_FALSE;
    } else {
        RETURN_LONG(ts);
    }
}
/* }}} */

 

strtotime函数在使用strtotime(“-1 month”)求上一个月的今天时会出一些状况,

因此也引出写这篇文章,本文包括如下内容:

strtotime函数的一些用法
strtotime函数的实现基本原理
strtotime(“-1 month”)求值失败的原因
strtotime函数的一些用法
1、 strtotime(“JAN”)和strtotime(“January”)

这两个用法的效果是一样的,都是返回指定月份的今天,如果指定月份没有今天,则顺延到下一个月。 如在2011-03-31计算二月,代码:

echo date("Y-m-d H:i:s", strtotime("feb", strtotime("2011-03-31")));
程序会输出: 2011-03-03 00:00:00。 从表象来看,这个结果也许不一定是我们想要的,但是这也算是一种解决方案,这种方案是由什么决定的呢? strtotime函数在执行月份的计算时只计算了月份,相当于直接将月份设置为指定的月份的值,而如jan,january都会有一个对应内部数值。

2、 first关键字

first是一个辅助型的关键字,它可以与星期,天等可以指定确认值的关键字组合使用,如求2011年的第一个星期天:

echo date("Y-m-d H:i:s", strtotime("second sunday", strtotime("2011-01-01"))), "<br />";
在PHP的源码中,对于first与星期和天的组合使用是分开的,即first day对应一个处理操作, 在最终的C实现中,天的值指定为1,即time结构中的d字段指定为1,如下代码:

 代码如下 复制代码
switch (time->relative.first_last_day_of) {
    case 1: /* first */
        time->d = 1;
        break;
    case 2: /* last */
        time->d = 0;
        time->m++;
        break;
}

3、previous和next关键字

与first类似,previous关键字可以与星期,天组合使用,表示指定时间的前一个星期几或前一天。如下所示代码:

echo date("Y-m-d H:i:s", strtotime("previous sunday", strtotime("2011-02-01"))), "<br />";
程序会输出:2011-01-30 00:00:00
程序求2011-02-01的前一个星期天。

next关键字与previous相反,它表示下一个星期几或后一天。

4、 last关键字

last关键字既可以作为上一个,也可以作为最后一个。如求上一个星期天的日期:

 代码如下 复制代码
echo date("Y-m-d H:i:s", strtotime("last sunday", strtotime("2011-02-05"))), "<br />";

程序会输出: 2011-01-30 00:00:00

当程序作为最后时,其应用场景是指定日期所在月的最后一天,相当于date(“t”)的结果。如求2000年2月的最后一天:

 代码如下 复制代码
echo date("Y-m-d H:i:s", strtotime("last day", strtotime("2000-02-01"))), "<br />";

first、previous、last和this关键字在re文件中属于同一组。

5、 back和front关键字

这两个关键字是对一天中的小时的向前和向后操作,其调用格式如下:

echo date("Y-m-d H:i:s", strtotime("back of 24", strtotime("2011-02-01"))), "<br />";
echo date("Y-m-d H:i:s", strtotime("front of 24", strtotime("2011-02-01"))), "<br />";
back表示将时间设置指定小时值的后一个小时的15分的位置。如果是24点,则算到第二天的0点15分。
front表示将时间设置指定小时值的前一个小时的45分的位置。如果是0点,则算前一天的23点45分。
上面的代码输出:2011-02-02 00:15:00 2011-02-01 23:45:00。 其中back of和front of后接的数组必须大于等于0并且小于等于24。

strtotime函数的实现基本原理
官方文档对于strtotime函数的说明是这样的:本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳 (自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。

这是一个标准PHP内置函数,从PHP4起就已经存在。strtotime函数是以一个扩展的方式加载进来的,在ext/date目录下有其全部实现。 作为一个标准的内置函数,其定义格式也是标准的,如下:

 代码如下 复制代码

PHP_FUNCTION(strtotime)
    //  处理输入,对于是否有第二个参数有没的处理

    //  调用相关函数,实现字符串的解析和结果计算

    //  返回结果
}

在输入处理中,先识别两个参数都存在的情况并进行处理,如果不是此种状态,则处理第二个参数不存在的情况, 如果都没有,则报错,返回FALSE。

strtotime函数的第一个参数是一个字符串,对于这个字符串,由于其复杂性,PHP使用了其词法解析一样的工具:re2c。在/ext/date/lib目录下,从parse_date.re文件我们可以看到其原始的re文件。 当用户以参数的形式传入一个字符串,此字符串将交给此程序处理,针对其字符串的不同,匹配不同的处理函数。 如strtotime(“yesterday”)调用,分析字符串时,将匹配yesterday字符串,此字符串对应函数如下:

 代码如下 复制代码

'yesterday'
{
    DEBUG_OUTPUT("yesterday");
    TIMELIB_INIT;
    TIMELIB_HAVE_RELATIVE();
    TIMELIB_UNHAVE_TIME();

    s->time->relative.d = -1;
    TIMELIB_DEINIT;
    return TIMELIB_RELATIVE;
}
这里有几个关键的结构体:

typedef struct Scanner {
    int           fd;
    uchar        *lim, *str, *ptr, *cur, *tok, *pos;
    unsigned int  line, len;
    struct timelib_error_container *errors;

    struct timelib_time *time;
    const timelib_tzdb  *tzdb;
} Scanner;

typedef struct timelib_time {
    timelib_sll      y, m, d;     /* Year, Month, Day */
    timelib_sll      h, i, s;     /* Hour, mInute, Second */
    double           f;           /* Fraction */
    int              z;           /* GMT offset in minutes */
    char            *tz_abbr;     /* Timezone abbreviation (display only) */
    timelib_tzinfo  *tz_info;     /* Timezone structure */
    signed int       dst;         /* Flag if we were parsing a DST zone */
    timelib_rel_time relative;

    timelib_sll      sse;         /* Seconds since epoch */

    unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day;

    unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
    unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
    unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */
    unsigned int   zone_type;    /*  1 time offset,
                                  *  3 TimeZone identifier,
                                  *  2 TimeZone abbreviation */
} timelib_time;

typedef struct timelib_rel_time {
    timelib_sll y, m, d; /* Years, Months and Days */
    timelib_sll h, i, s; /* Hours, mInutes and Seconds */

    int weekday; /* Stores the day in 'next monday' */
    int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */

    int first_last_day_of;
    int invert; /* Whether the difference should be inverted */
    timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */

    timelib_special  special;
    unsigned int   have_weekday_relative, have_special_relative;
} timelib_rel_time;

s->time->relative.d = -1;所表示的意思是当前时间的相对天数是-1。 这只是中间词法解析的中间结果,但是最后结果是通过这些中间结果计算出来的。

strtotime(“-1 month”)求值失败的原因
虽然strtotime(“-1 month”)这种方法对于后一个月比前一个月的天数的情况会求值失败,但是从其本质上来说,这并没有错。 PHP这样实现也无可厚非。只是我们的需求决定了我们不能使用这种方法,因此我们称其为求值失败。

我们来看它的实现过程,由于没有第二个参数,所以程序使用默认的当前时间。 第一个参数传入的是-1 month字符串,这个字符串所对应的re文件中的正则为:

 代码如下 复制代码

reltextunit = (('sec'|'second'|'min'|'minute'|'hour'|'day'|'fortnight'|'forthnight'|'month'|'year') 's'?) | 'weeks' | daytext;

relnumber = ([+-]*[ \t]*[0-9]+);
relative = relnumber space? (reltextunit | 'week' );

最终relative会对应一系列操作,程序会识别出前面的-1 和后面的month字符串,month对应一种操作类型:TIMELIB_MONTH。 在此之后,根据识别出来的数字和操作类型执行操作,如下代码:

 代码如下 复制代码
case TIMELIB_MONTH:  s->time->relative.m += amount * relunit->multiplier; break;

如上代码,则是直接记录月份的相对值减一。 但是对于类似于3月31号这样的情况,2月没有31号,程序会自动将日期计算到下一个月。

文章来实现一个PHP编码转换函数mb_convert_encoding与iconv用法,希望例子能帮助到各位。

将一个短信接口代码从apache迁移到nginx+php-fpm后,发现无法发出短信了,查看php日志,

[25-Sep-2014 20:15:21] WARNING: [pool www] child 9617 said into stderr: “NOTICE: PHP message: PHP Fatal error:  Call to undefined function mb_convert_encoding() in /data/htdocs/xx.php on line 13″

发现函数mb_convert_encoding没定义,看着像某个模块没装,google了把,要装个mbstring扩展,之前都是一下装好多扩展(虽然不知道这个扩展是干啥的,按照网络文档来),现在是要应用需要哪个装哪个,逼格略有提高(至少这样能让我知道哪个模块是干啥的)。

mb-convert-encoding
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )

将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。

官网文档  http://php.net/manual/zh/function.mb-convert-encoding.php 需要安装mbstring扩展库,如果已经编译好的php可以这样热编译下

 代码如下 复制代码


cd /tmp/php-5.3.28/ext/mbstring/
usr/local/services/php/bin/phpize
./configure --with-php-config=/usr/local/services/php/bin/php-config
make && make install
vim /usr/local/services/php/etc/php.ini
extension="/usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/mbstring.so";
 iconv
string iconv ( string in_charset, string out_charset, string str )

iconv函数库能够完成各种字符集间的转换
注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
Returns the converted string or FALSE on failu

官网地址  http://php.net/manual/zh/book.iconv.php

已经安装好php的,同样也可以使用上面的方法安装iconv模块

mb_convert_encoding例子
mb_convert_encoding这个函数是用来转换编码的。英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码

 
做一个GBK To UTF-8

 代码如下 复制代码
<?php
header(“content-Type: text/html; charset=Utf-8″);
echo mb_convert_encoding(“???S我的友仔”, “UTF-8″, “GBK”);
?>

再来个GB2312 To Big5

 代码如下 复制代码

<?php
header(“content-Type: text/html; charset=big5″);
echo mb_convert_encoding(“你是我的朋友”, “big5″, “GB2312″);
?>

mb_strtolower() – 使字符串小写
mb_strtoupper() – 使字符串大写
strtolower() – 将字符串转化为小写
strtoupper() – 将字符串转化为大写
ucfirst() – 将字符串的首字母转换为大写
ucwords() – 将字符串中每个单词的首字母转换为大写
 

iconv例子

把gb2312置换成utf-8:

 代码如下 复制代码

$text=iconv("GB2312","UTF-8",$text);

在用$text=iconv(“UTF-8″,”GB2312″,$text)过程中,如果遇到一些特别字符时,如:”—”,英文名中的”.”等等字符,转换就断掉了。这些字符后的文字都没法继续转换了。

针对这的问题,可以用如下代码实现:

 代码如下 复制代码

$text=iconv("UTF-8","GBK",$text);

你没有看错,就这么简单,不使用gb2312,而写成GBK,就可以了。

还有一种方法,第二个参数,加上//IGNORE,忽略错误,如下:

 代码如下 复制代码

iconv("UTF-8","GB2312//IGNORE",$data);

一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数。

 代码如下 复制代码


$content = iconv("GBK", "UTF-8″, $content);
$content = mb_convert_encoding($content, "UTF-8″, "GBK");

PHP curl函数是可以模仿用户进行访问页面了,下面来给各位介绍一下工作中常用的到的PHP curl函数示例

PHP有着很好的curl机制,但是用起来总是不那么的令人满意,因为它需要好几步的操作和,好多难记的参数,这里我将php的curl封装了一下,使它不那么的复杂:

 代码如下 复制代码

<?php
/**
 * php模拟curl请求
 *
 * @param string $url     请求的url
 * @param string $method  请求的方法, 默认POST
 * @param array  $data    请求传递的数据
 * @param array  $header  请求设置的头信息
 * @param int    $head    是否打印头信息
 * @param int    $body    是否打印body信息
 * @param int    $timeout 设置超时时间
 *
 * @return array
 */
function curl($url,$method="POST",$data=array(),$header=array(),$head=0,$body=0,$timeout = 30)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if (strpos($url, "https") !== false ) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        }
    }
    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    switch ($method) {
    case 'POST':
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        break;
    case 'GET':
        break;
    case 'PUT':
        curl_setopt($ch, CURLOPT_PUT, 1);
        curl_setopt($ch, CURLOPT_INFILE, '');
        curl_setopt($ch, CURLOPT_INFILESIZE, 10);
        break;
    case 'DELETE':
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        break;
    default:
        break;
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, $head);
    curl_setopt($ch, CURLOPT_NOBODY, $body);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rtn = curl_exec($ch); //获得返回
    if (curl_errno($ch)) {
        echo 'Errno'.curl_error($ch);//捕抓异常
    }
    curl_close($ch);
    return $rtn;
}
?>

在php中serialize()与unserialize()函数是一对函数,下面本文章就来为各位同学介绍serialize()与unserialize()函数的使用例子,希望能帮助到各位。

php函数serialize():

这个函数作用就是序列化数据,返回一个可存储的字符串,该函数有利于存储或传递PHP的值,同时不丢失其类型和结构。所以我们在cms数据库里面经常看到这样的结构。

一般,我们将复杂或者数据量多而没有必要分开存储的数据封装成一个多维数组通过serialize()转成字符串,然后存进数据库,需要的时候再拿出来转成数组再用,而拿出了转成数组用的就是php的unserialize(),前面多了一个un嘛。直接上例子:

 代码如下 复制代码

$a=array("周","满","和","个人","博客","www.111cn.net","周满和","个人博客");
$b=serialize($a);
print_r($b);
echo "<br/>";
$c=unserialize($b);
print_r($c);

输出结果是:

//经过serialize序列化之后得到的数据,也是存这个进数据库
a:8:{i:0;s:3:"周";i:1;s:3:"满";i:2;s:3:"和";i:3;s:6:"个人";i:4;s:6:"博客";i:5;s:20:"www.111cn.net";i:6;s:9:"周满和";i:7;s:12:"个人博客";}
 
//经过unserialize()反序列化得到的数据,和之前的$a数组结构一样吧
Array ( [0] => 周 [1] => 满 [2] => 和 [3] => 个人 [4] => 博客 [5] => www.111cn.net [6] => 周满和 [7] => 个人博客 )

在上一个复杂一点的二维数组的例子:

 代码如下 复制代码

$a1=array(
    'name'=>array("周","满",'和'),
    'name_weburl'=>array("周满和","www.111cn.net"),
    'all'=>array('周满和个人博客'=>'www.111cn.net')
);
$b1=serialize($a1);
print_r($b1);
echo "<br/>";
$c1=unserialize($b1); 
print_r($c1);

输出结果是:


//经过serialize序列化之后得到的数据,也是存这个进数据库
a:3:{s:4:"name";a:3:{i:0;s:3:"周";i:1;s:3:"满";i:2;s:3:"和";}s:11:"name_weburl";a:2:{i:0;s:9:"周满和";i:1;s:20:"www.111cn.net";}s:3:"all";a:1:{s:21:"周满和个人博客";s:20:"www.111cn.net";}}
 
//经过unserialize()反序列化得到的数据,和之前的$a数组结构一样吧
Array ( [name] => Array ( [0] => 周 [1] => 满 [2] => 和 ) [name_weburl] => Array ( [0] => 周满和 [1] => www.111cn.net ) [all] => Array ( [周满和个人博客] => www.111cn.net ) )

[!--infotagslink--]

相关文章