Skip to content

Instantly share code, notes, and snippets.

View tqyq's full-sized avatar

altman tqyq

  • Earth
View GitHub Profile
@tqyq
tqyq / tidyHTML.php
Last active September 11, 2015 01:33
tidyHTML php format html to dom
function tidyHTML($buffer) {
// load our document into a DOM object
$dom = new DOMDocument();
// we want nice output
$dom->preserveWhiteSpace = false;
$dom->loadHTML($buffer);
$dom->formatOutput = true;
return($dom->saveHTML());
}
@tqyq
tqyq / php mongodb query
Last active September 11, 2015 01:32
mongodb native query in thinkphp framework
public function test()
{
$u = D('User')->getCollection()->find(['$or'=>[['mobile'=>'13888888888'], ['name'=>'test1']]]);
foreach ($u as $v) {
json_out($v);
}
$u = D('User')->getCollection()->distinct('name');
json_out($u);
}
@tqyq
tqyq / nginx upload file proxy_pass
Created September 11, 2015 01:42
nginx upload file proxy_pass to another nginx with php-fpm config
worker_processes 1;
events {
multi_accept on;
worker_connections 1024;
}
http {
client_max_body_size 30m;
@tqyq
tqyq / ajaxupload using ajaxupload.js
Last active November 30, 2015 02:30
ajaxupload using ajaxupload.js
$(function() {
new AjaxUpload("up_btn", {
action: "/home/api/upload",
autoSubmit: true,
name: "bin",
data: {},
responseType: 'json',
onChange: function (file, extension) {
},
onSubmit: function (file, extension) {
@tqyq
tqyq / javascript string format
Last active November 30, 2015 02:30
javascript string format
String.prototype.format = function()
{
var args = arguments;
return this.replace(/\{(\d+)\}/g,
function(m,i){
return args[i];
});
}
// sample
@tqyq
tqyq / php mongodb aggregate group query
Last active November 30, 2015 02:29
php mongodb aggregate group query
public function group()
{
$r = D('Charge')->getCollection()->aggregate([
['$match'=>['cat'=>'55d17590f7c6d19c1e000039']],
['$group' => ['_id' => '$cat', 'money' => ['$sum' => '$price'], 'oc' => ['$sum' => 1]]],
['$sort' => ['money' => -1]]
]);
json_out($r);
}
@tqyq
tqyq / php calculate distance between lat, lng loc
Last active November 30, 2015 02:29
php calculate distance between lat, lng loc
function getDistance($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 6367000; //approximate radius of earth in meters
$lat1 = ($lat1 * pi()) / 180;
$lng1 = ($lng1 * pi()) / 180;
$lat2 = ($lat2 * pi()) / 180;
$lng2 = ($lng2 * pi()) / 180;
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
@tqyq
tqyq / php mongodb geo index query by loc
Last active April 7, 2016 08:15
php mongodb geo index query by loc
// run below in mongo client shell
db.teacher.createIndex( { "loc": "2d" } )
// php query
public function test($dis=1) {
$r = D('Teacher')->command([
'geoNear'=>'teacher',
'near'=>[116.32723, 39.977331],
'maxDistance' => $dis/111,
'distanceMultiplier' => 111,
@tqyq
tqyq / php get http header value by key
Last active November 30, 2015 02:28
php get http header value by key
function head_value($key) {
$headers = [];
if (!function_exists('getallheaders')) {
foreach ($_SERVER as $name => $value) {
/* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */
if (strtolower(substr($name, 0, 5)) == 'http_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
} else {
@tqyq
tqyq / php generate json tree from array
Last active June 28, 2018 01:35
php generate json tree from array
function genTree($items, $idkey='_id'){
$tree = array();
foreach($items as $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$item[$idkey]];
}else{
$tree[] = &$items[$item[$idkey]];
}
}
return $tree;