PHP中json_encode、json_decode与serialize、unserialize

 更新时间:2016年11月25日 15:55  点击:1465

json_encode和json_decode的效率并没有比serialize和unserialize的效率高,在反序列化的时候性能相差两倍左右,PHP 5.3执行效率比PHP 5.2略有提升。

 代码如下 复制代码


<?php教程
$target = array (
'name' => '全能头盔',
'quality' => 'Blue',
'ti_id' => 21302,
'is_bind' => 1,
'demand_conditions' =>
array (
'HeroLevel' => 1,
),
'quality_attr_sign' =>
array (
'HeroStrength' => 8,
'HeroAgility' => 8,
'HeroIntelligence' => 8,
),
);
$json = json_encode($target);
$seri = serialize($target);
echo "json : " . strlen($json) . " ";
echo "serialize : " . strlen($seri) . " ";
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_encode($target);
}
$etime = microtime(true);
echo "json_encode : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
json_decode($json);
}
$etime = microtime(true);
echo "json_decode : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
serialize($target);
}
$etime = microtime(true);
echo "serialize : " . ($etime - $stime) . " ";
//----------------------------------
$stime = microtime(true);
for ($i = 0; $i < 10000; $i ++)
{
unserialize($seri);
}
$etime = microtime(true);
echo "unserialize : " . ($etime - $stime) . " ";
echo 'DONE.';
?>

我们先来分析流程

一,数据库教程,

二,数据库文件dbconn.php

三,vote.php投票页面

四,投票功能页面vote_add.php

五,查看所有投票数据viewpoll.php

现在我们来看看数据库结构

 代码如下 复制代码

drop database if exists vote;
create database vote;
use vote;

CREATE TABLE  title  (                                                                                                                                                                                                                                                                
           id  int(6) NOT NULL auto_increment,
    username  varchar(20) NOT NULL,
    email  varchar(50) NOT NULL,
           title  varchar(250) NOT NULL default '',                                                                                                                                                                                                                                           
           types  tinyint(2) NOT NULL default '1',
    choice text,
           times  datetime NOT NULL default '0000-00-00 00:00:00',                                                                                                                                                                                                                            
          primary KEY ( id )     
        );

CREATE TABLE  vote  (                                                                                                                                                                                                                                                
           id  int(6) NOT NULL auto_increment,                                                                                                                                                                                                                         
           titleid  int(6) NOT NULL default '0',                                                                                                                                                                                                                           
           info  text NOT NULL,                                                                                                                                                                                                                                              
           vcount  int(4) NOT NULL default '0',                                                                                                                                                                                                                        
          primary KEY ( id )                                                                                                                                                                                                                                             
        );
create table email (
    id int(6) not null,
    email varchar(50) not null,
    primary key ( id, email)
 );

数据库连接文件

 代码如下 复制代码

<?php
$conn=mysql_connect("localhost","phpdb","phpdb")
        or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("vote",$conn) or die ("不能选择数据库: ".mysql_error());

?>

 

 //方法一

 代码如下 复制代码
 $str = '<a href="http://www.111cn.net" target="_blank" name="doc3_p"><img src="" onload="setImgSize(this,170,170);"></a>';
    preg_match_all ('|^<a href="(.*)".*|U',$str,$out, PREG_PATTERN_ORDER);
    print_r($out);


 
 //方法二
 

 代码如下 复制代码
 preg_match_all('/href=['"]+(.*)['"]+/',$str,$matches);
 var_dump($matches[1]);


 
 
 //方法三

 代码如下 复制代码
 //(?<=<as*)href=[^ '">]+


 
 //方法四

 代码如下 复制代码
 preg_match("/<as*href="(.*?)"[^>]*>.*?</a>/is", $str, $aMatch);
 print_r($aMatch[1]);


 
 //方法五

 代码如下 复制代码
 preg_match_all('/href=('|"|s)*([^'">s]+)/i',$str,$match);
 print_r($match);
 
 preg_match_all('/src=('|"|s)*([^'">s]+)/i',$str,$match);
 print_r($match);


 /*
 上面这正则得到url的地址都是用了php正则表达试来实现的,只是方法有一点不同了。

/*
字符串A:"1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457",
例如我想知道这个字符串里面是否含有"2",这时候"12","20","22"等
*/
//一

 代码如下 复制代码
if(in_array('2',explode(',',$str))

//二

 代码如下 复制代码
$str= '1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457';
if(strpos(',' . $str . ',', ',2,') !== false)
{
 //TODO
}

//三

 代码如下 复制代码
echo preg_match('/(?<=^|,)2(?=,|$)/','1,3,4,5,6,7,8,9,10,11,12,14,15,17,20,22,123,457');

 

//方法一

 代码如下 复制代码
echo preg_replace('#[x{4e00}-x{9fa5}]#ue','chinese_unicode("\0")',"您好,中国");//保证"您好,中国"是utf-8。
function chinese_unicode($c) {
        return "u".dechex(((ord($c[0]) & 0x1f) << 12) + (ord($c[1]) & 0x3f << 6) + (ord($c[2]) & 0x3f));


}
//方法二

 代码如下 复制代码
foreach(unpack(
'n*',
mb_convert_encoding('你好', 'unicode', 'gbk')
) as $i) {
echo 'u',dechex($i);


}

 

[!--infotagslink--]

相关文章