• PHP中IP与整型互相转换

    IP转换成整型存储是数据库优化一大趋势,不少人目前存储IP时还在使用字符串类型存储,字符串索引比整型索引消耗资源很多,特别是表中数据量大的时候,以及求查询某一个IP段的数据,今天说的IP是指IPv4,IPv6不在本文范围内。

    系统函数ip2long与long2ip

    PHP中有内置函数ip2long可以将ip地址转换整型。

    $ip = '210.110.11.49';
    echo ip2long($ip);
    
    // Output: -764540111
    

    输出的整型有负号是因为我们得到的结果是有符号整型,有符号整型最大值2147483647,要把结果转换为无符号型可以这么写

    ...

    READ ALL

  • 基于jQuery写的一个Dialog插件

    基于jQuery写的一个弹窗插件,功能简陋,效果单一,主要是是用jQuery的animate函数做同时滑动淡出淡入效果

    JavaScript Code

    (function($) {
      $.fn.dialog = function (param) {
        if (typeof param.dialog === 'undefined') {
          return;
        }
    
        var dialog = param.dialog;
        var close = param.close || '.close';
        var speed = 400;
        var margin_left = '-'+parseInt($(dialog).width()/2)+'px';
        var margin_top = '-'+parseInt($(dialog).height()/2)+'px';
        var _this = null; 
        var bg = '<div class="dialog_bg" style="width:100%;height:'+$(document).height()+'px;background:#000;opacity:0.7;filter:alpha(opacity=70);  position:absolute;left:0;top:0;z-index:2147483600;display:none;"></div>';
    
        $(dialog).css({
          position: 'fixed',
          'margin-left': margin_left,
          'margin-top': margin_top,
          left: '50%',
          top:'50%',
          display: 'none',
          'z-index': 2147483601
        });
    
        $('body').append(bg);
    
        $(this).each(function(){
          _this = $(this);
          _this.click(function(){
            if (!$(dialog).is(':visible')) {
              $('.dialog_bg').fadeIn(parseInt(speed/2));
              $(dialog).css({'top':'35%','display':'block','opacity':0.0});
              $(dialog).animate({top:'50%',opacity:1},speed);
            }
          });
    
          $(dialog+' '+close).click(function(){
            $(dialog)
              .animate({
                top: '65%',
                opacity:0
              },speed,false,function() {
                $(this)
                  .hide()
                  .css('top','50%');
                $('.dialog_bg').fadeOut(parseInt(speed/2));
              });
          });
        });
      }
       
    })(jQuery);
    

    ...

    READ ALL

  • Python石头剪子布简单程序

    我之前写过一篇基于JS的石头剪子布程序 《JavaScript写的一个石头剪子布游戏,大家进来玩玩吧!》,今天又基于Python写了一个实例,这里边的算法有点特殊但一时也想不到一个好的算法规律。

    Python Code

    # encoding=UTF-8
    # 石头剪子布 程序
    import random
     
    # 定义石头剪子布字典
    dict = {1:'剪子',2:'石头',3:'布'}
     
    for row in dict:
      print '编号:',row,' = ',dict[row]
     
    print '您出什么?'
     
    loop = True
    while loop:
      you = raw_input('请输入编号回车: ')
      try:
        you = int(you)
        if you>=1 and you<=3:
          loop = False
        else:
          print '请输入 1-3 范围内的编号'
      except Exception,e:
        print '请输入正确的数字编号'
     
    dn = random.randint(1,3)
    print '你出:',dict[you]
    print '电脑出:',dict[dn]
    print '结果:',
     
    if dn==you:
      print '平局'
    elif (you>dn and you-dn==1) or you+2==dn:
      print '你胜'
    else:
      print '电脑胜'
    

    ...

    READ ALL

  • shell执行PHP传入参数的三种方式

    有些时候需要在shell命令下执行PHP脚本,比如定时任务。这就涉及到在shell命令下如何给PHP传参的问题,通常有三种方式传参

    使用$argv $argc参数接收

    <?php
    /**
     * 使用 $argc $argv 接受参数
     */
     
    echo "接收到{$argc}个参数";
    print_r($argv);
    

    Example

    [root@DELL113 lee]# /usr/local/php/bin/php test.php
    接收到1个参数Array
    (
      [0] => test.php
    )
    [root@DELL113 lee]# /usr/local/php/bin/php test.php a b c d
    接收到5个参数Array
    (
      [0] => test.php
      [1] => a
      [2] => b
      [3] => c
      [4] => d
    )
    [root@DELL113 lee]#
    

    ...

    READ ALL

  • 基于jQuery开发的焦点图插件

    同事找帮忙需要写一个普通的焦点图,切换效果是渐变,默认不自动切换,图片有小点,点击小点可以切换图片。花了三个多小时写好,贴出来的目的是让那些开发jQuery插件的童鞋做个参考。

    源码

    focus.js

    /**
     * 焦点图
     * 2013-03-11
     */
    (function($) {
      $.fn.focus = function(param){   
        // 默认高度
        var height = param.height || 200;   
        // 默认宽度
        var width = param.width || 100; 
        var _this = null;
    
        // jQuery的对象选择器是多个,所以这里使用each遍历每一个节点对象
        $(this).each(function(){
    
          // 把每一个节点对象赋给一个变量,这有助于程序效率
          _this = $(this);
    
          $('body').css({margin: 0, padding: 0});
    
          // 改变盒子大小 
          _this.css({
            width: width + 'px',
            height: height+'px',
            position: 'relative',
            padding: 0
          });
        
    
          var fock_png_width = 16;            // png宽度(px)
          var fock_png_margin_sx = 8;         // png 小图标上下间距(px)
          var ul_height = fock_png_width + fock_png_margin_sx * 2; // 底部黑条的高度(px)
          var box_height = _this.height();            
          var box_width = _this.width();
          var fock_png_margin_zy = 10;            // png 小图标左右间距
          var js = document.scripts;
          var speed = 500;
          var sper_url = '';              // js路径
          var sper_file_name = 'sper.png';        // png 文件名
          var last_index = 0;
          var lock = false;
    
          var objImg = _this.find('>img');     // 查找所有图片
    
          if (objImg.length === 0) {
            return false;   
          }else{
            var img_length = objImg.length; 
          }
    
          // 获取js文件路径 目的是为了使用png图片
          for (var i = js.length; i>0; i--) {
            if (js[i-1].src.indexOf('lee_plug')>-1) {
              sper_url = js[i-1].src.substring(0, js[i-1].src.lastIndexOf("/") + 1) + sper_file_name;
            }
          }
    
          // 底部黑条     
          _this.append('<div style="position:absolute;top:'+(height-ul_height)+'px;background:#000;opacity:0.7;height:'+ul_height+'px;margin:0 auto;padding:0;width:100%;"></div>');
    
          // 创建小图标
          var html = '<ul style="position:absolute;top:'+(height-ul_height)+'px;height:'+ul_height+'px;margin:0 auto;padding:0;width:100%;opacity:0.7;">';
          for (var i=0; i < img_length; i++) {
            $(objImg[i]).css({
              width: '100%',
              height: '100%',
              position: 'absolute',
              margin: 0,
              padding: 0
            });
    
            if (i >= 1) {
              $(objImg[i]).hide();        
              var sper_weizhi = 0;
            }else{
              var sper_weizhi = '-' + fock_png_width + 'px';
            }
    
            var left = parseInt((box_width - img_length*(fock_png_width+fock_png_margin_zy*2)) /2) + i*(fock_png_width+fock_png_margin_zy);
    
            html += '<li style="display:block;position:absolute;width:'+fock_png_width+'px;height:'+fock_png_width+'px;background:url('+sper_url+') 0 '+sper_weizhi+' no-repeat;margin:0; padding:0;cursor:pointer; top:'+parseInt((ul_height-fock_png_width)/2)+'px;left:'+left+'px;"></li>';
          }
          html += '</ul>';
    
          _this.append(html);
    
          // 绑定单击事件
          _this.find('>ul>li').each(function(i){
            // 这里使用的js原生事件绑定,因为jQuery 的 click怎么闭包传入i没有弄明白,所以使用原生函数处理
            $(this).get(0).onclick = function (num, obj) {
              return function(){
                if (num === last_index || lock) {
                  return false;
                }
                lock = true;
                obj.find('>ul>li').each(function (i) {
                  $(this).css('background', 'url(' + sper_url + ') 0 0 no-repeat');
                });
                obj.find('>ul>li:eq(' + num + ')').css('background','url(' + sper_url + ') 0 -' + fock_png_width + 'px no-repeat');
                obj.find('>img:visible').fadeOut(speed);
                obj.find('>img:eq(' + num + ')').fadeIn(speed, function () {
                  lock = false;
                  last_index = num;   
                });
              }
            }(i, _this); 
          });
        }); 
      };      
      
    })(jQuery);
    

    ...

    READ ALL

  • SQLServer CONVERT()用法疑问

    SQLServer CONVERT()函数是格式化时间,在做时间对比时很有用。但CONVERT()函数的第二个参数虽然是日期类型,但不接受字符串拼接的格式

    什么是CONVERT()

    CONVERT()函数是把日期转换为新数据类型的通用函数。此函数可以用不同的格式显示日期/时间数据。

    语法

    CONVERT(data_type(length),data_to_be_converted,style)

    • data_type(length) 规定目标数据类型(带有可选的长度)
    • data_to_be_converted 含有需要转换的值
    • style规定日期/时间的输出格式。

    ...

    READ ALL

  • PHP系统函数eval()使用介绍

    什么是eval

    eval()函数把字符串按照PHP代码来运算。 该字符串必须是合法的PHP代码,且必须以分号结尾。 如果没有在代码字符串中调用return语句,则返回NULL。如果代码中存在解析错误,则eval()函数返回false

    语法

    eval(phpcode)

    例子1

    <?php
    $string = "beautiful";
    $time = "winter";
     
    $str = 'This is a $string $time morning!';
    echo $str. "<br />";
     
    eval("\$str = \"$str\";");
    echo $str;
    ?>
    

    ...

    READ ALL

  • SQLServer读取库里所有表名

    在SQLServer里要获取库中所有表名,并不能像MySQL那样直接show table就搞定

    mysql> use demo
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
     
    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_demo |
    +----------------+
    | a              |
    | b              |
    | c              |
    +----------------+
    3 rows in set (0.00 sec)
    

    ...

    READ ALL

  • SQLServer巧用datediff获得时间戳

    找了大半天,发现SQLServer貌似没有内建函数读取时间戳。只好自己动手写了一个

    1> SELECT datediff(ss,'1970-01-01',GETDATE()) as time
    2> go
    time
    1361988498
    (1 row affected)
    1>
    

    datediff取差函数

    • ss 表示以秒为单位
    • 1970-01-01 时间戳都从这个日期计算
    • getdate() 当前时间

    既读取当前时间与1970-01-01之间的秒数就是当前时间戳

    datediff函数很强大,比如要算出两个日期之间的月数

    ...

    READ ALL

  • Python对象的深拷贝与浅拷贝

    本文内容是在《Python核心编程2》上看到的,感觉很有用便写出来,给大家参考参考!

    浅拷贝

    首先我们使用两种方式来拷贝对象,一种是切片,另外一种是工厂方法。然后使用id函数来看看它们的标示符

    # encoding=UTF-8
     
    obj = ['name',['age',18]]
    a = obj[:]
    b = list(obj)
    for x in obj,a,b:
      print id(x)
    

    Output:

    35217032
    35227912
    29943304
    

    他们的id都不同,按照正常的判断,三个不同id的对象应该都是独立的。那么我们先给他们改改名看看

    ...

    READ ALL