Skip to content

Instantly share code, notes, and snippets.

View lepig's full-sized avatar
🎯
Focusing

Geek Cho lepig

🎯
Focusing
  • Global
View GitHub Profile
func fibonacci() func() int {
a, b := -1, 1
return func() int {
a, b = b, a+b
return b
}
}
func main() {
f := fibonacci()
@lepig
lepig / observer.php
Created November 2, 2017 08:24
PHP观察者模式
<?php
//被观察者接口
interface ObservableInterface {
public function attach(ObserverInterface $observer);
public function detach(ObserverInterface $observer);
public function notify();
}
//被观察者 (订单、用户登录、注册)
// 这个Observable可以换成Order来理解
class Observable implements ObservableInterface {
@lepig
lepig / getDistance.php
Created October 31, 2017 03:02
根据2个经纬度计算2点间的距离
<?php
/**
* 根据两点间的经纬度计算距离
* @param $lng1
* @param $lat1
* @param $lng2
* @param $lat2
* @return int
*/
@lepig
lepig / 100light.php
Last active September 26, 2017 08:32
100盏灯问题
<?php
$nums = 2; //按开关的次数
for ($i = 1; $i <= 100; $i++) {
$arr[$i] = 0;
}
//这个数组生成可以使用下面的自带函数
//$arr=array_fill(1,100,0); //填充100盏灯都是关闭状态
$count = count($arr);
@lepig
lepig / MultiArraySort.php
Created September 7, 2017 01:58
PHP多维数组按不同字段进行排序
<?php
/**
* PHP数组不定长多键值排序
*
* @author Zjmainstay
* @website http://www.zjmainstay.cn, https://glot.io/snippets/ernfkfnjhb
* @param array $list 数据源
* @param array $rules 排序规则 ['key1'=>'asc', 'key2' => 'desc', ...]
* @return array
*/
@lepig
lepig / log.php
Created May 30, 2017 15:17
PHP简单日志记录类库
PHP写日志文件需要打开、写入和关闭文件等操作,PHP有fopen(),fwrite()和fclose()三个函数与之对应,而另一个函数file_put_contents()它也能字符串写入文件,其实这个函数实现了依次调用 fopen(),fwrite() 以及 fclose()。所以我们使用file_put_contents()非常简洁。值得注意的是,往文件后面追加内容时需要带上参数:FILE_APPEND。
实际运行中,我们有可能会遇到日志文件超大的情况,所以我们设置一个最大值,当日志文件大小超过这个最大值时,将此日志文件备份好,然后重新生成一个新的日志文件来记录新的日志内容。
在写日志前,我们将日志内容进行json格式化,所以需要将内容转化成JSON格式,然后写入文件。当然你也可以不用json,或者换作别的工具程序(如日志分析工具)可以阅读的格式。总之,我们写入的内容是方便必要时可以方便读取。
<?php
/*
* 日志类
* 每天生成一个日志文件,当文件超过指定大小则备份日志文件并重新生成新的日志文件
*/
@lepig
lepig / glob.php
Last active May 30, 2017 08:38
glob函数递归读取文件列表
<?php
/**
* glob递归读取某个目录下的文件
*/
function glob_recursive($pattern, $flags = 0)
{
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
{
@lepig
lepig / readBigFile.php
Last active May 30, 2017 08:14
PHP读取大日志文件方法
<?php
foreach( glob( ngx_log. "/*.log" ) as $file ) {
$log = new SplFileObject($file);
foreach( $log as $line ){
$ipnum = getip($line);
if( $ipnum ) {
$ips[ $ipnum ] = (int)$ips[ $ipnum ] + 1;
//这里可以做一些数据入库处理
@lepig
lepig / getAgeByBirthday.php
Last active April 10, 2017 10:39
原型如下,使用的时候可以省略time()以及用一个变量先存储一下strtotime($birthday)得到的值
<?php
/**
* 通过出生日期获取具体年龄
* @param type date
* @return type number
*/
function get_age($birthday)
{
$age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
@lepig
lepig / deepcate.php
Created March 19, 2017 12:50
PHP无限级分类示例代码
/**
* 此方法由@Tonton 提供
* http://my.oschina.net/u/918697
* @date 2012-12-12
*/
function genTree5($items) {
foreach ($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
return isset($items[0]['son']) ? $items[0]['son'] : array();
}