• 2015-05-07

    PHP decode C string-like encoded string

    Views: 24403 | No Comments

    To decode string encoded in C string-like to what it exactly was in memory, I write a PHP function.

    C string-like encoded string as like this:

    \r\n\x90
    

    The PHP function, unescape C string.

    function unescape_c_str($str){
        $ret = '';
        $len = strlen($str);
        for($i=0; $i<$len; $i++){
            if($str[$i] != '\\'){
                $ret .= $str[$i];
                continue;
            }
            $i++;
            switch($str[$i]){
                case 's':
                    $ret .= ' ';
                    break;
                case 'a':
                    $ret .= "\a";
                    break;
                case 'b':
                    $ret .= "\b";
                    break;
                case 'f':
                    $ret .= "\f";
                    break;
                case 'v':
                    $ret .= "\v";
                    break;
                case 'r':
                    $ret .= "\r";
                    break;
                case 'n':
                    $ret .= "\n";
                    break;
                case 't':
                    $ret .= "\t";
                    break;
                case '\\':
                    $ret .= "\\";
                    break;
                case 'x':
                    $hex = substr($str, $i+1, 2);
                    $ret .= hex2bin($hex);
                    $i += 2;
                    break;
                default:
                    $ret .= $str[$i];
                    break;
            }
        }
        return $ret;
    }
    
    Posted by ideawu at 2015-05-07 12:54:20
  • 2013-01-01

    PHP working with LevelDB

    Views: 16965 | No Comments

    The Google LevelDB is a C/C++ key-value storage library, this means you can only write C/C++ code to use LevelDB. Unlike other key-value storage solution and MySQL, LevelDB doesn’t have a network server.

    If you wan your PHP code to work with LevelDB, you can either wrap LevelDB as a PHP module, or wrap LevelDB as a network server. Writing a PHP LevelDB module is not a good idea most of the time, because we always want to connect to one LevelDB from several machines across network.

    This is why I create SSDB – a LevelDB server with zset data type support. SSDB has a very simple network protocol, that can be implemented with many programming language, such as C, C++, PHP, Python, Cpy…

    Here is a example of PHP operates with SSDB(LevelDB):

    <?php
    require_once('SSDB.php');
    $ssdb = new SimpleSSDB('127.0.0.1', 8888);
    $resp = $ssdb->set('key', '123');
    $resp = $ssdb->get('key');
    echo $resp; // output: 123
    

    Download SSDB from: https://github.com/ideawu/ssdb

    Posted by ideawu at 2013-01-01 01:16:20
  • 2012-09-19

    Always converting PHP’s floating numbers to string with sprintf

    Views: 10900 | No Comments

    I think PHP’s way to deal with float is too BAD!

    PHP code:

    $a = 12345678.123456;
    echo (string) $a; echo "\n";
    echo $a.''; echo "\n";
    echo sprintf('%f', $a); echo "\n";
    

    Outputs:

    12345678.1235
    12345678.1235
    12345678.123456
    
    Posted by ideawu at 2012-09-19 11:42:37
  • 2012-07-17

    PHP codes to dump Redis

    Views: 15552 | No Comments

    Here is a script of PHP codes to dump Redis data, from one db to another. Usage: save codes to file named a.php, change $from and $to, run:

    php a.php

    <?php
    
    $from = '127.0.0.1:6200/6';
    $to = '127.0.0.1:6200/8';
    
    $from_redis = redis_init($from);
    $to_redis = redis_init($to);
    
    $keys = $from_redis->keys('*');
    $count = 0;
    $total = count($keys);
    foreach($keys as $key){
        if(++$count % 100 == 1){
            echo "$count/$total\n";
        }
        $type = $from_redis->type($key);
        switch($type){
            case Redis::REDIS_STRING:
                $val = $from_redis->get($key);
                $to_redis->set($key, $val);
                break;
            case Redis::REDIS_LIST:
                $list = $from_redis->lRange($key, 0, -1);
                foreach($list as $val){
                    $to_redis->rPush($key, $val);
                }
                break;
            case Redis::REDIS_HASH:
                $hash = $from_redis->hGetAll($key);
                $to_redis->hMSet($key, $hash);
                break;
            case Redis::REDIS_ZSET:
                $zset = $from_redis->zRange($key, 0, -1, true);
                foreach($zset as $val=>$score){
                    $to_redis->zAdd($key, $score, $val);
                }
                break;
        }
    }
    
    function redis_init($conf){
        $redis = new Redis();
        preg_match('/^([^:]+)(:[0-9]+)?\\/(.+)?/', $conf, $ms);
        $host = $ms[1];
        $port = trim($ms[2], ':');
        $db = $ms[3];
        $redis->connect($host, $port);
        $redis->select($db);
        return $redis;
    }
    
    Posted by ideawu at 2012-07-17 15:29:08 Tags:
|<<<1>>>| 1/1 Pages, 4 Results.