• 投稿
  • 私信
  • 存档

Code Recoder

  • 文字

    1 March, 2012

    MySQL:获取最新修改的记录中auto_increase字段的值

    利用 LAST_INSERT_ID() 函数获取最新update或insert记录的auto_increase字段值,一般为id。例:

    向users表中插入一条记录

    insert into users(username, password) values('test', '1234')

    获取刚新添加记录的ID,并将其插入到另一个表

    insert into auth(user_id, regtime) values(LAST_INSERT_ID(), now())
    全文
  • 文字

    1 March, 2012

    PHP:正则表达式过滤微博文体中的话题和对象名

    $post_content = "@tvrcgo和@twitter在研究用#PHP#的#正则表达式#过滤话题和对象名";
     
    $tag_pattern = "/\#([^\#|.]+)\#/";
    preg_match_all($tag_pattern, $post_content, $tagsarr);
    $tags = implode(',',$tagsarr[1]);
     
    $user_pattern = "/\@([a-zA-z0-9_]+)/";
    $post_content = preg_replace($user_pattern, '<a href="http://twitter.com/${1}">@${1}</a>', $post_content );
    $post_content = preg_replace($tag_pattern, '<a href="http://twitter.com/search?q=#${1}">#${1}#</a>', $post_content);

    感谢uper的投稿 :)

    全文
  • 文字

    29 February, 2012

    PHP:数组与对象的相互转换

    PHP:数组与对象的相互转换

    class oop {
        /*
        将数组转化成对象
        toObject        : Transforms an array into an object filtering it
        $source         : Array to transform
        $currentLevel : See $maxLevels
        $maxLevels   : Protect the system in case of lots of recursion
        i.e. <input type="text" name="test[][]....[N]"
        */
       
        public static function toObject(
            $source=array(),
            $array=array(),
            $maxLevels=3,
            $currentLevel=0) {
            if ( !sizeof($source) || ($currentLevel > $maxLevels) ) return FALSE;
            $array   =  (sizeof($array)) ? $array : $source;
            $obj     =   oop::arrToObj($array);
            return $obj;
        }
        function arrToObj($array){
            if(!sizeof($array))return false;
            $obj = new stdClass();
            foreach($array as $key=>$val){
                if(is_array($val)){
                    oop::arrToObj($val);
                    continue;
                }
                $obj->$key=$val;
            }
            return $obj;
        }
    }
    全文
  • 文字

    29 February, 2012

    php,curl获取header信息

    function get_header($url){
        $ch  = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY,true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($ch, CURLOPT_AUTOREFERER,true);
        curl_setopt($ch, CURLOPT_TIMEOUT,30);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: */*',
        'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
        'Connection: Keep-Alive'));
        $header = curl_exec($ch);
        return $header;
    }
    1 热度 全文
The End.