Skip to content

Instantly share code, notes, and snippets.

View flowerains's full-sized avatar
😉
ruby is best

Alex Wang flowerains

😉
ruby is best
View GitHub Profile
@flowerains
flowerains / memcache-hash.php
Created March 20, 2017 16:55
缓存的普通hash和一致性hash的算法
<?php
// 整数hash
function intHash($key)
{
$md5 = substr(md5($key), 0, 8);
$seed = 31;
$hash = 0;
for ($i = 0;$i < 8; ++$i) {
$hash = $hash * $seed + ord(md5($i));
@flowerains
flowerains / time.php
Created January 6, 2016 08:15
判断时间戳来输出刚刚/分钟前/小时前/昨天/时间 time
<?php
function T($time) {
//获取今天凌晨的时间戳
$day = strtotime(date('Y-m-d',time()));
//获取昨天凌晨的时间戳
$pday = strtotime(date('Y-m-d',strtotime('-1 day')));
//获取现在的时间戳
$nowtime = time();
$tc = $nowtime-$time;
@flowerains
flowerains / is_mobile.php
Last active January 6, 2016 08:13
判断是否为手机端 is_mobile
<?php
public static function is_mobile() {
//正则表达式,批配不同手机浏览器UA关键词。
$regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
$regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT'])); //如果UA中存在上面的关键词则返回真。
@flowerains
flowerains / sort.php
Last active December 14, 2015 04:31
二维数组排序 sort
<?php
//指定数组以$keys键值排序
function array_sort($array,$keys,$type='asc'){
//$array为要排序的数组,$keys为要用来排序的键名,$type默认为升序排序
$keysvalue = $new_array = array();
foreach ($array as $k=>$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
@flowerains
flowerains / http_post.php
Created September 15, 2015 07:42
一个PHP原生的发送POST请求的方法
<?php
$url = 'http://h.bilibili.com/api/pushS';
$data = array('act' => 'getHidInfo', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
@flowerains
flowerains / my.cnf
Created May 15, 2015 02:19
mysql配置文件
[mysqld]
basedir=/usr/local/Cellar/mysql/5.6.24
pid-file=/usr/local/var/mysql/flowerainsdeMacBook-Pro.local.pid
datadir=/usr/local/var/mysql
plugin-dir=/usr/local/Cellar/mysql/5.6.24/lib/plugin
port=3306
socket= /usr/local/var/mysql/mysql.pid
#server_id = <ALEXMYSQL>
@flowerains
flowerains / pagination.js
Last active January 6, 2016 08:08
一个分页方法,非常的实用 pagelist
var nowAlid, //现在类型
pageSize = 50, //默认每页数量
nowPage = 1, //当前页数
pagiWidth = 5; //最大页数宽度
function pageList(result){
var pageDiv = $("#paginationDiv");
var __GetPageList = function(result, pageDiv) {
var total_page = result.totalPage;
var now_page = result.page;
@flowerains
flowerains / captcha.php
Last active January 6, 2016 08:08
captcha 一个很简单的验证码插件
<?php
//随机生成一个4位数的数字验证码
session_start();
$num="";
$num=mt_rand(1000,9999);
//4位验证码也可以用rand(1000,9999)直接生成
//将生成的验证码写入session,备验证页面使用
$_SESSION["Checknum"] = $num;
//创建图片,定义颜色值
@flowerains
flowerains / array_map.php
Last active August 29, 2015 14:18
array_map,将回调函数作用到给定的数组的值上
<?php
if (get_magic_quotes_gpc()) {
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
}
array_map('trim',explode("\n",$a));
@flowerains
flowerains / console.js
Created March 7, 2015 19:24
console.js,看见的一个非常好的小插件,感谢作者"颜海镜"
;(function(g) {
'use strict';
var _console = g.console || {};
var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'exception', 'error', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
var console = {version: '0.1.0'};
var key;
for(var i = 0, len = methods.length; i < len; i++) {
key = methods[i];
console[key] = function (key) {