2009/02/25

cookie读写类

JavaScript语言: cookie操作代码

smartclick_Cookie = {

    get: function(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = smartclick_Cookie.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    },
    set: function(key, value, ttl, path, domain, secure) {
        cookie = [key+'='+    encodeURIComponent(value),
                   'path='+    ((!path   || path=='')  ? '/' : path),
                   'domain='+  ((!domain || domain=='')?  window.location.hostname : domain)];
       
        if (ttl)         cookie.push( 'expires='+smartclick_Cookie.hoursToExpireDate(ttl));
        if (secure)      cookie.push('secure');
       
        return document.cookie = cookie.join('; ');
    },

    //@param integer    ttl        Time To Live (hours)
    hoursToExpireDate: function(ttl) {
        if (parseInt(ttl) == 'NaN' ) return '';
        else {
            now = new Date();
            now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
            return now.toGMTString();
        }
    },
    trim: function( text ) {
        return (text || "").replace( /^\s+|\s+$/g, "" );
    }
}

几点说明:
1.ttl生存期时间单位是小时。
2.get方法的原理是字符串匹配,对[key=]value的中括号部分进行匹配,优于使用正则进行匹配,正则如有特殊字符的情况会很麻烦。

2009/02/21

周末看了两部电影,歌舞青春3和史密斯夫妇


晚上拖着看了看《歌舞青春3》,比较喜欢这部个中文名字,还是比较感慨老美的,能拍出这样的电影。记得大宝曾经说过,青春是要挥霍的。年少时候还是感性一点嘛,这样以后能够多回忆一些东西。




《史密斯夫妇》,恩,属于老早就上映了自己一直没看的电影。08暑假才看《疯狂的石头》,觉得宁浩很NB,所以《疯狂的石头》一上映就看,肯定不后悔。喜欢朱利皮特,好莱坞模范夫妻啊,是不是挺羡慕的?


Posted by Picasa

2009/02/18

最近看了本书《赚钱机器,Money Machine》

笔记

1.财富的定义:停止你现在的工作,能够继续生存多久?

2.做有积累性的工作。无积累性的工作:有十年出租车驾龄的司机和一天驾龄的司机的价格是一样的,别人不会因为你开了十年而多付钱。

3.使用你的钱来赚钱。

4.终极目标:打造属于你的赚钱机器,最终得到财富自由和时间自由。

5.没有捷径,勤奋努力的工作吧(个人想法)。

晚上两个小时瞎倒腾,原来IE设置了代理。。。

现象
1.写python使用urllib2模块,出现如下错误:urllib2.URLError: <urlopen error (10054, 'Connection reset by peer')>。

2.朋友汇款,使用招行专业版无法登陆,具体表现为,“通讯故障(#12):(*5)请检查您的通讯线路”。

检测网络,傲游浏览器一直可以上网、试试火狐也可以、飞信一直可以使、ping命令可以ping通,纳闷啊,反复google,检查本机服务,登陆路由器更改设置、更改网络连接、关闭防火墙、切换有线无线。。。未果,后来无意点开IE,无法打开网页,不会是设置了代理了吧?果然是!顿时泪奔,一晚上就浪费了啊,还以为python有问题,真可以把IE拖出去斩了!

2009/02/14

堆排序,C版


#include <stdio.h>
#include <math.h>


#define LEFT(i)        ((i)<<1)
#define RIGHT(i)    (((i)<<1) + 1)

void max_heapify(int a[], int i, int heapsize);
void heap_sort(int a[], int heapsize);
void build_max_heap(int a[], int heapsize);
void exchange(int *x, int *y);

//交换两个数的值
void exchange(int *x, int *y) {
    int temp;

    temp = *x;
    *x = *y;
    *y = temp;
}

//保持最大堆性质
void max_heapify(int a[], int i, int heapsize) {
    int left, right, largerest;

    left = LEFT(i);
    right = RIGHT(i);

    if (left <= heapsize && a[left]>a[i])
    {
        largerest = left;
    }else{
        largerest = i;
    }

    if (right <= heapsize && a[right]>a[largerest])
    {
        largerest = right;
    }

    if(largerest != i) {
        exchange(&a[i], &a[largerest]);
        max_heapify(a, largerest, heapsize);
    }

}

//建造最大堆
void build_max_heap(int a[], int heapsize) {
    int i;

    for (i=(int)ceil(heapsize/2); i >=1 ; i--)
    {
        max_heapify(a, i, heapsize);
    }
}

//堆排序
void heap_sort(int a[], int heapsize) {

    //build heap
    build_max_heap(a, heapsize);

    while(heapsize>1)
    {
        //exchange max
        exchange(&a[1], &a[heapsize]);
        heapsize--;
        max_heapify(a, 1, heapsize);
    }
   
}

int main() {
    int a[] = {0,    2,        0,        23,        38,        19,
                    98,        203,    803,    3,        78,
                    34,        68,        49,        9,        55};
   
    int array_size = sizeof(a)/sizeof(int);
    int i,heapsize;

    heapsize = array_size - 1;

    heap_sort(a, heapsize);
   
    //打印排序后的数组
    for (i=1; i<=heapsize ; i++)
    {
        printf("%d\t",a[i]);
    }
    printf("\n");
    getchar();
    return 0;

}

2009/02/06

支持中文的截取字符函数,不同编码下中文字符的范围一目了然

有时候经常需要查找某个编码下中文字符的编码范围,下面这个函数一目了然,省事!

PHP语言: 中文截取

<?php
/*
* 中文截取,支持gb2312,gbk,utf-8,big5
*
* @param string $str 要截取的字串
* @param int $start 截取起始位置
* @param int $length 截取长度
* @param string $charset utf-8|gb2312|gbk|big5 编码
* @param $suffix 是否加尾缀
*/
public function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);

    $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";

    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix) return $slice."…";
   
    return $slice;
}
?>