杂记


/**
* @Author: Marte
* @Date: 2017-12-19 13:31:56
* @Last Modified by: Marte
* @Last Modified time: 2017-12-21 16:01:09
*/
/*********************用PHP追加数组,使用array_merge_recursive(),将两个数组合并在一起,注意,与array_merge()函数是不一样的,array_merge()的两个数组有重复项时会覆盖掉,而array_merge_recursive()则不会。array_merge_recursive()语法*/

array array_merge_recursive(array array1,array array2[…,array arrayN])
// 下面是一个PHP追加数组的例子:
$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// 输出结果:
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )

/****************************************************PHP JSON_ENCODE 不转义中文汉字的方法**********************************************************************/
// 原创 2014年07月25日 17:56:54 标签:PHP /JSON 47702
// ios程序中不识别读取到的JSON数据中 \u开头的数据。
// PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据。
// 网上很多,但是其实都是错误的,正确的方法是在json_encode 中加入一个参数 JSON_UNESCAPED_UNICODE
echo json_encode($result,JSON_UNESCAPED_UNICODE);

/*******************************thinkphp 自带分页**********************************/
$perpage = 2;
$Page = new \Think\Page($count,$perpage);// 实例化分页类传入总记录数和每页显示的记录数(2)
$Page->setConfig('prev' , "上一页");
$Page->setConfig('next' , "下一页");
$Page->setConfig('first' , "首页");
$Page->setConfig('last' , "尾页");
$Page->lastSuffix = false;
$Page->rollPage = 5;
$show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性

/*#################################################################计 算 年 龄*******###########################************************/
$this->getAge($now,$value['boday']);

public function getAge($now,$birthday) {
$age = 0;
$year = $month = $day = 0;
if (is_array($birthday)) {
extract($birthday);
} else {
if (strpos($birthday, '-') !== false) {
list($year, $month, $day) = explode('-', $birthday);
$day = substr($day, 0, 2); //get the first two chars in case of '2000-11-03 12:12:00'
}
}
$age = date('Y') - $year;
if (date('m') < $month || (date('m') == $month && date('d') < $day)) $age--;
return $this->diffDate($now,$birthday,$age);
}
public function diffDate($date1,$date2,$sui){
$datestart= date('Y-m-d',strtotime($date1));
if(strtotime($datestart)>strtotime($date2)){
$tmp=$date2;
$date2=$datestart;
$datestart=$tmp;
}
list($Y1,$m1,$d1)=explode('-',$datestart);
list($Y2,$m2,$d2)=explode('-',$date2);
$Y=$Y2-$Y1; // 1
$m=$m2-$m1; // 0
$d=$d2-$d1; // -11
if($d<0){
$d+=(int)date('t',strtotime("-1 month $date2"));
$m=$m--;
}
if($m<0){
$m+=12;
$y=$y--;
}
if($sui == 0 && $m == 0 && $d != 0){
return $d.'天';
}elseif($sui == 0 && $m != 0 && $d != 0){
return $m.'个月零'.$d.'天';
}elseif($sui != 0 && $m == 0 && $d != 0){
return $sui.'岁零'.$d.'天';
}elseif($sui != 0 && $m < 10 && $m !=0 && $d != 0){
return $sui.'岁 零'.$m.'个月'.$d.'天';
}else{
return $sui.'岁'.$m.'个月'.$d.'天';
}
}
/*###################################################################遍历文件夹下的文件进行操作############################*/
public function read_all($dir){
if(!is_dir($dir)) return false;
$handle = opendir($dir);
if($handle){
while(($fl = readdir($handle)) !== false){
$temp = iconv('GBK','utf-8',$dir.DIRECTORY_SEPARATOR.$fl);//转换成utf-8格式
//如果不加 $fl!='.' && $fl != '..' 则会造成把$dir的父级目录也读取出来
if(is_dir($temp) && $fl!='.' && $fl != '..'){
echo '目录:'.$temp.'<br>';
$this->read_all($temp);
}else{
if($fl!='.' && $fl != '..'){
// echo '文件:'.$temp.'<br>';
$filesize =filesize($temp);
if($filesize > 212339){
//D:\phpStudy\WWW\myshop\UpPhoto\20180123033716hgg7yl.png
$name=explode('\\', $temp);
$com = A("Common");
// var_dump($name);die;
$com->image_png_size_add($temp,'C:\phpStudy\WWW\myshop\UpPhoto2\thum\\'.$name[6]);
unlink("$temp");
}
}
}
}
}
}
/*##########################################################筛选出表记录的图片(正则匹配符合条件的两者之间的字符串)#########################################*/
//$pattern = '#http://120.25.221.30/myshop/Public/Image/(.*?)#i';如果不要结束符号,就仅仅是获取你你写出来的部分的字符串
// $pattern = '#http://120.25.221.30/myshop/Public/Image/(.*?)"#i'; //这是的正则指的是从 http://120.25.221.30/myshop/Public/Image/开始到 " 之间的所有字符串
// $items = preg_match_all($pattern, $data['json'], $matches);
public function copy2(){
$json = M("Json");
$data = $json->field("json")->select();
foreach ($data as $key => $value) {
$pattern = '#http://(.*?)"#i';
$items = preg_match_all($pattern, $value['json'], $matches);
// var_dump($matches[1]);die;
foreach ($matches[1] as $k => $v){
$va = explode('/',$v);
$va = end($va);
copy("./UpPhoto/".$va,"./UpPhoto/thumb/".$va);
unlink("./UpPhoto/".$va);
}
}
}
/*#######################################################将表里面的base64图片换成URL地址的图片(正则匹配到相符条件的字符串将其替换)########################################################################*/
public function huan($id){
$json = M("Json");
$data = $json->where("id={$id}")->getField("json");
// var_dump($data);
$pattern = '#data:image/(.*?)"#i';
$items = preg_match_all($pattern, $data, $matches);
if($items >0){
$images =array();
foreach ($matches[0] as $key => $value) {
$va = explode(',',$value);
$img = base64_decode($va[1]);
$index = A('Index');
$thumb = $index->rand3(5);
$a = file_put_contents("./UpPhoto/".$thumb.".jpeg", $img);//返回的是字节数
$images[$key] = "http://www.qzriji.com/UpPhoto/".$thumb.".jpeg\"";
}
$shuju = str_replace($matches[0],$images,$data);
$con = array('json'=>$shuju);
$result = $json->where("id={$_POST['id']}")->save($con);
}
}

/*###########################################过滤敏感字##############################################*/
public function legal($name){
$str = file_get_contents('./Public/key.txt');
$str = (trim($str));
$jin = explode('|', $str);
$allergicWord = $jin;
$str = $name;
$str = strtr($str, array(' '=>''));
for ($i=0;$i<count($allergicWord);$i++){
$content = substr_count($str, $allergicWord[$i]);
if($content>0){
$info = $content;
break;
}
}
if($info>0){
return 1;
}
}
$this->legal($_POST['username']);
if($pan >0 || strlen($_POST['username']) ==0){
$msg = array('cate'=>0,'msg'=>'非法昵称');
echo json_encode($msg);
return;
}
public function seeing(){
$json = M("Json");
$result = $json->where()->find();
// var_dump($result);die;
if($result['status'] =='0'){
$data =array('status'=>1);
}elseif($result['status'] =='1'){
$data =array('status'=>0);
}
$result2 = $json->where("id={$_POST['id']}")->save($data);
if($result2){
$info =array('cate'=>1);
echo json_encode($info);
}else{
$info =array('cate'=>0);
echo json_encode($info);
}
}
public function copy(){
$json = M("Json");
$data = $json->field("headpic,id")->select();
// var_dump($data);die;
$thumbs = array();
foreach ($data as $key => $value) {
// $pattern = '#http://120.25.221.30/myshop/UpPhoto/(.*?)#i';
// // var_dump($value);die;
// $items = preg_match_all($pattern, $value['json'], $matches);
// var_dump($matches);die;
foreach ($matches[0] as $k => $v) {
$thumbs[$k] = "http://www.qzriji.com/UpPhoto/";
}
//替换多个字符串str_replace函数 数组只能是一维数组
$shuju = str_replace($matches[0],$thumbs,$value['json']);
$con = array('json'=>$shuju);
$result = $json->where("id={$value['id']}")->save($con);
}
}
/*########################################去空格#########################################################*/
$_POST['goods_name'] = preg_replace('# #','',$_POST['goods_name']);

/*##################################判断数字#############################*/
if(preg_match("/^\d*$/",$_POST['goods_name'])){
echo('是数字');
}else{
echo('不是数字');
}
/*######################################合并音频############################################################*/
public function tece(){
exec("C:/fft/bin/ffmpeg -f concat -i ./Wechat/weixinrecord/filelist.txt -c copy ./Wechat/weixinrecord/output.mp3");
echo 111;
}
具体参考 http://blog.csdn.net/xiaojun111111/article/details/52438230

public function upload2(){
// echo 11;die;
$media = $_POST['media_id'];
$med = explode(',',$media);
foreach ($med as $key => $value) {
$media_id = $value;
$access_token = getAccessToken();

$path = "./Wechat/weixinrecord/"; //保存路径,相对当前文件的路径
$outPath = "./php/Wechat/weixinrecord/"; //输出路径,给show.php 文件用,上一级

if(!is_dir($path)){
mkdir($path);
}
//微 信上传下载媒体文件
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
$name = "wxupload_".time().rand(1111,9999);
$filename = $name.".amr";
$this->downAndSaveFile($url,$path."/".$filename);

exec("C:/fft/bin/ffmpeg -i ./Wechat/weixinrecord/".$filename." ./Wechat/weixinrecord/".$name.".mp3");
$data["path"] = $outPath.$filename;
$data["msg"] = "download record audio success!";
// $data["url"] = $url;

echo json_encode($data);
}
}

/*########################################################确认删除功能*##################################################################*/

<a title="删除" href="javascript:;" onclick="product_del(this,'{$v.id}')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>

function product_del(obj,id) {
var msg = "您真的确定要删除吗?\n\n请确认!";
if (confirm(msg)==true){
$.ajax({
type: 'POST',
url:'http://www.qzriji.com/Admin/Bookdub/ajaxDel',
data:{'id':id},
dataType: 'json',
success: function(data){
if(data.cate =='0'){
alert(data.msg);
}
if(data.cate =='1'){
alert(data.msg);
}
window.location.reload();
},
error:function(data) {

},
});
}else{
return false;
}
}


public function ajaxDel($id){
// var_dump($id);

$model = D('Bookdub');
$book = M('Bookdub');
$story = M('Story');
$audio = M('Bookaudio');
$pan = $audio->where("title_id={$id}")->select();
if($pan){
$info = array('cate'=>0,'msg'=>'有用户在使用这故事,不能删除');
echo json_encode($info);
}
// var_dump($pan);
$au = $story->where("id={$id}")->find();
// var_dump($au);
$img2=explode('/',$au['url']);
$mus2 = end($img2);
$result2 = unlink("./Public/Uploads/book/".$mus2);

$data = $book->where("title_id={$id}")->select();
// var_dump($data);die;
foreach ($data as $key => $value) {
$img=explode('/',$value['thumb']);
$mus = end($img);
$result = unlink("./Public/Uploads/book/".$mus);

$aui=explode('/',$value['model']);
$audi = end($aui);
$res = unlink("./Public/Uploads/book/audio/".$audi);
}
$result = $book->where("title_id={$id}")->delete();
$result3 = $story->where("id={$id}")->delete();
$info = array('cate'=>1,'msg'=>'删除成功');
echo json_encode($info);
}
/*###################################################################跨模块#######################################*/
//User为控制器 number为方法
R('User/number');
R('Admin/User/number');
R('shop://Admin/User/number');
---------------------------------------------------------------------------------------------------------------------------------
// 微信获取用户信息的两个接口和两个ACCESS_TOKEN
// 有一段时间没有搞微信开发了 ,今天突然要改一下程序! 回头一看 微信的帮助文档太tm的稀烂的,太难懂了,这做个笔记以后看着方便
// 微信有2个ACCESS_TOKEN,
// 1,基础接口的token 获取接口是
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
// 2,用户网页授权access_token 获取接口地址是
// https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
// 网页授权access_token 需要通过code去获取
// code是怎么来的,是通过调用下面接口来获取的
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

// 注意这个接口中有个参数scope 默认有2个值snsapi_base和snsapi_userinfo,这个接口会根据scope 来生成不同的code并且获取不同作用的access_token ,不管scope传什么值都能在得到对应access_token的同时得到open_id, 如果你只需要得到opend_id 那使用snsapi_base参数到此结束了,如果需要获取用户的其他信息比如 昵称 地址 就要snsapi_userinfo 会弹出授权

// 3 怎么获取用户信息那就调用下面接口

// https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN

// 很明显这个接口中的access_token是第二步获取code的时候scope 参数传snsapi_userinfo来换取的access_token

// 4 微信还有一个获取用户基本信息的接口 但是 这个接口需要你关注了公众号

// https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN (此接口的access_token 是接口基础调用access_token 不是网页授权access_token)

// 微信的解释:是在用户和公众号产生消息交互或关注后事件推送后,才能根据用户OpenID来获取用户基本信息。这个接口,包括其他微信接口,都是需要该用户(即openid)关注了公众号后,才能调用成功的。


/*############################################PHPphp保存微信用户头像到本地或者服务器的完美方案!#######################################################################*/
https://blog.csdn.net/slyjit/article/details/78955884
//方法一://推荐用该方法
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$url='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKkGpNuUhaBniatRsiaG7ksqmhUWzkk40kTRS6icQS7kJcsfxcibQo7vDFcKibr7NHb9YIXiaXsEtLcdL6A/0';
$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);$data = curl_exec($curl);$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);curl_close($curl);
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}
$img_content=$imgBase64Code;//图片内容
//echo $img_content;exit;
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result))
{
$type = $result[2];//得到图片类型png?jpg?gif?
$new_file = "./cs/cs.{$type}";
if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content))))
{ echo '新文件保存成功:', $new_file; }}

//方法二://该方法比较消耗服务器资源,慎用!
$url='http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKkGpNuUhaBniatRsiaG7ksqmhUWzkk40kTRS6icQS7kJcsfxcibQo7vDFcKibr7NHb9YIXiaXsEtLcdL6A/0';
$img_file = file_get_contents($url); $img_content= base64_encode($img_file);
$type = 'jpeg';//得到图片类型png?jpg?gif? $new_file = "./cs/cs.{$type}";
if (file_put_contents($new_file, base64_decode($img_content)))
{
echo 'ok', $new_file;
}

原文地址:https://www.cnblogs.com/lvtiansong/p/9490519.html