Skip to content

Instantly share code, notes, and snippets.

View huzemin's full-sized avatar
🐱
CAONIMA

胡泽民 huzemin

🐱
CAONIMA
  • HuiZhou University
  • HuiZhou ,China
View GitHub Profile
@huzemin
huzemin / generate_random_str.php
Last active January 8, 2016 02:22
Generate random string ( use to generate a password)
<?php
if(!function_exists('generate_random_str')) {
function generate_random_str($len = 9, $type=2) {
$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$numbers = '0123456789';
$special_chars = '!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$words = '';
switch ($type) {
case 1:
$words = $letters.$numbers;
@huzemin
huzemin / cal_run_time.php
Created January 12, 2016 08:31
Calculate what the function execute spend.
<?php
/**
* 计算函数的执行时间,可以用于性能统计
* @param $function
* @param array $params
* @param int $times
* @return mixed
*/
@huzemin
huzemin / get-arr-val.php
Last active January 22, 2016 04:09
a fast way to get array value by key.
<?php
/**
* 快速获取数据的值
* @param $soure
* @param $key
* @param string $separetor
* @return array|null
* @throws Exception
*/
function _v($soure, $key, $separetor = '.') {
@huzemin
huzemin / ishadowsocks.serverConfig.js
Created January 18, 2016 10:19
Get free shadowsocks server from ishadowsocks.com. [Node.js]
/**
* Get free shadowsocks from ishadowsocks.
* the server password will change every 6 hours.
* ------------------------------------
* @author huzemin8 <[email protected]>
*/
var cheerio = require('cheerio');
var fs = require('fs');
var path = require('path');
@huzemin
huzemin / size_normalize.js
Last active January 18, 2016 14:30
Normalize file size description
/**
* @author huzemin8 <[email protected]>
*/
/**
* @param in_size mixed file size
* @param type string output size
* @param precision int
* @return string
@huzemin
huzemin / Client.js
Created January 19, 2016 13:05
Create a simple real time chat by node.
var net = require('net');
var readline = require('readline');
var HOST = process.env.IP,
PORT = process.env.PORT;
var me = readline.createInterface(process.stdin, process.stdout);
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
@huzemin
huzemin / get_host_ip.js
Created January 21, 2016 05:03
获取本机IP
var os = require('os');
// 获取网卡信息
var networkInterfaces = os.networkInterfaces();
function getHostIp() {
// 可能会用多个活动网卡的情况,这样也会出现多个IP
var ip = [];
for(var key in networkInterfaces) {
@huzemin
huzemin / download_simple_file.js
Created January 22, 2016 04:08
下载网络文件
var http = require('http');
var url = require('url');
var debug = require('debug')('download');
var fs = require('fs');
var path = require('path');
debug('booting %s', "download.js")
var url_parms = url.parse("http://img2.chouti.com/CHOUTI_8CEB537A0B9E46BDA7B744ABED8C3812_W163H163=C200x200.jpg");
var options = {
protocol: url_parms.protocol,
@huzemin
huzemin / getBoundary.js
Created January 23, 2016 06:00
获取HTTP POST表单提交数据的Boundary
function getBoundary(req) {
if (req && req.headers) {
var content_type = req.headers['content-type'].split(';');
if (content_type.length > 1) {
var raw_boundary = content_type.pop().split('=');
var boundary = raw_boundary[1];
return boundary;
}
}
else {
@huzemin
huzemin / filter_array.php
Created January 25, 2016 06:18
获取数组中特定键的数据,并进行默认值设置
<?php
/**
* @author 胡泽民 <[email protected]>
*/
// 获取数组中特定键的数据,并进行默认值设置
function filter_array($params, $keys) {
if(!is_array($params)) {
return null;
}