js常用数据操作函数库

1.js操作cookie

js原生操作cookie
document.cookie=”name=”+username;
编写函数
//写cookies
function setCookie(name,value)
{
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + “=”+ escape (value) + “;expires=” +    exp.toGMTString();
}
//读cookie
function getCookie(name)
{
var arr,reg=new RegExp(“(^| )”+name+”=([^;]*)(;|$)”);
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
//删除cookie
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() – 1);
var cval=getCookie(name);
if(cval!=null)
document.cookie= name + “=”+cval+”;expires=”+exp.toGMTString();
}

2.js获取url 

有时候会出现编码错误导致的乱码
function get_url_var(name)
{
var reg = new RegExp(“(^|&)”+ name +”=([^&]*)(&|$)”);
var r = window.location.search.substr(1).match(reg);
if(r!=null)return  unescape(r[2]); return null;
}

3.jq  ajax

$.ajax({
url:url
data:{
‘ui_cardnum’:ui_cardnum
},
type:’post’,
dataType:’json’,

success:function(data){

 

},
error:function(data) {
console.log(data);
}
});

 

4.js  操作localstorage

  • 获取键值:localStorage.getItem(“key”)
  • 设置键值:localStorage.setItem(“key”,”value”)
  • 清除键值:localStorage.removeItem(“key”)
  • 清除所有键值:localStorage.clear()

php 数据处理相关代码片段

一.2V数组转树形 代码片段

 

数据模型

 

$items = array(
1 => array(‘id’ => 1, ‘pid’ => 0, ‘name’ => ‘江西省’),
2 => array(‘id’ => 2, ‘pid’ => 0, ‘name’ => ‘黑龙江省’),
3 => array(‘id’ => 3, ‘pid’ => 1, ‘name’ => ‘南昌市’),
4 => array(‘id’ => 4, ‘pid’ => 2, ‘name’ => ‘哈尔滨市’),
5 => array(‘id’ => 5, ‘pid’ => 2, ‘name’ => ‘鸡西市’),
6 => array(‘id’ => 6, ‘pid’ => 4, ‘name’ => ‘香坊区’),
7 => array(‘id’ => 7, ‘pid’ => 4, ‘name’ => ‘南岗区’),
8 => array(‘id’ => 8, ‘pid’ => 6, ‘name’ => ‘和兴路’),
9 => array(‘id’ => 9, ‘pid’ => 7, ‘name’ => ‘西大直街’),
10 => array(‘id’ => 10, ‘pid’ => 8, ‘name’ => ‘东北林业大学’),
11 => array(‘id’ => 11, ‘pid’ => 9, ‘name’ => ‘哈尔滨工业大学’),
12 => array(‘id’ => 12, ‘pid’ => 8, ‘name’ => ‘哈尔滨师范大学’),
13 => array(‘id’ => 13, ‘pid’ => 1, ‘name’ => ‘赣州市’),
14 => array(‘id’ => 14, ‘pid’ => 13, ‘name’ => ‘赣县’),
15 => array(‘id’ => 15, ‘pid’ => 13, ‘name’ => ‘于都县’),
16 => array(‘id’ => 16, ‘pid’ => 14, ‘name’ => ‘茅店镇’),
17 => array(‘id’ => 17, ‘pid’ => 14, ‘name’ => ‘大田乡’),
18 => array(‘id’ => 18, ‘pid’ => 16, ‘name’ => ‘义源村’),
19 => array(‘id’ => 19, ‘pid’ => 16, ‘name’ => ‘上坝村’),
);

函数

 

function genTree9(Array $items) {

$tree = array();    //格式化好的树
foreach ($items as $item)
if (isset($items[$item[‘pid’]]))
$items[$item[‘pid’]][‘son’][] = &$items[$item[‘id’]];
else
$tree[] = &$items[$item[‘id’]];
return $tree;

}

二.传输过程中 汉字不转uncode

json_encode($data, JSON_UNESCAPED_UNICODE)

三.带有特殊字符的url转码

 

urlencode($data)

 

如  http://weixin.com?XXXX?a=1&b=222&c=666/d=777/…

 

其中? & /将被转义

四.get和post请求

get

function get_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//运行curl,结果以json形式返回
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

//post请求
function http_request($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output=curl_exec($curl);
curl_close($curl);
return $output;
}

file_get_content()函数也可以作get获取内容 亦可以用作读取网络文件