开发者

php gettype fix

Following function I created is to store data in a database with the correct type.

I have several questions:

  1. It makes sense to me to turn empty strings in to NULL. Are there consequences I am not considering?
  2. What is a good max_length value for sqlite3 text. I know blob and text length constrains are the same, but are there any other considerations like displaying the data in a shell, etc.
  3. Is there a simple way to check if data is an image, and what type, since mime is not available at this stage?
  4. Does something stick out that I am missing?

    public static function getType($value, $max_length = 50){
    
        $type = gettype($value);
    
        if($type == 'NULL'
                || $type == 'boolean'
            开发者_StackOverflow    || $type == 'integer'
                || $type == 'double'
                || $type == 'object'
                || $type == 'resource'
                || $type == 'array'
            )
            return array('type'=>$type,'value'=>$value);
    
        if($type == 'string' && empty($value))
            return array('type'=>'NULL','value'=>$value);
    
        if($type == 'string' && strlen($value) > $max_length)
            return array('type'=>'blob','value'=>$value);
    
        if($type == 'string' && substr($value, 0,1) === '0')
            return array('type'=>'string','value'=>$value);
    
        if($type == 'string' && is_numeric($value)){
            $int   = (int) $value;
            $float = (float) $value;
    
            if($int == $value){
                $value = $int;
                $type = 'integer';
            }elseif($float == $value){
                $value = $float;
                $type = 'double';
            }
        }elseif($type == 'string'){
            return array('type'=>$type,'value'=>$value);
        }else{
            $type = 'blob';
        }
        return array('type'=>$type,'value'=>$value);
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜