手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2011年08月的文章

买了几本书

这两天买了三本书,作为公司的资料。
head first设计模式,这本书相对是从浅入深,还不错
代码大全2精华本,这本就相对较简单了。。
编程珠玑,虽然做WEB开发看这个书好象有点浪费,但其实不然,还是可以看看的。

其他同事买了点算法、数据结构类的书,到时候也可以抢来看看。
我自己是买了head first的另外两本书,一本是项目管理的,一本是ios开发,感觉好象都是浅显易懂。

最近用脑图用多了一点,画画啥的都方便很多了,看来以后出规划可以直接用脑图出了

Tags: 设计模式

nbscala-2.8.x

说实话,这个插件等了很久了。自从NB升级到7.0了,原来的scala插件就再也不能用了,即使用了以前的插件,也无法在新版的NB下运行。
直到前一阵子才发现,原来,终于有人更新了这个插件。。
怕多介绍了之后会影响别人的想法,我也不能过分主观的说太多这个插件的优点,就平常的介绍一下下吧。官网地址是:http://plugins.netbeans.org/plugin/36598/nbscala-2-8-x

官方的介绍就一句话:Scala plugin for NetBeans 7.0, works with Scala 2.8.x
附了一张小图片:
大小: 31.01 K
尺寸: 373 x 154
浏览: 1812 次
点击打开新窗口浏览全图

想用的人可以开始用了。

Tags: netbeans

郁闷啊,我的PR值

真TMD纠结,为了这么备案,我现在真是吐血了。
原来的PR5都6、7年了,原来的备案都也6、7年,现在可好,备案突然消失,PR也降了1。
在这个神奇的国度,还有什么不能理解的,想想我也麻木了。

主要还是国外的VPS等不太方便,事实上我的站也几乎都保持着稳定,所以要再等等,如果顺利的话,还是准备迁移了。否则再这么折腾个几次,我是受不了了。

求理解。

Tags: pr

扩展yii的验证类

在项目中遇到文件上传的问题,这时候需要用到CFileValidator,但是官方的验证中少了一点点的处理,比如,对于图片,我只想上传尺寸正好是300x500的图片。怎么办?
所以我做了一点小小的扩展,于是我在rules里面加了这么两条:

PHP代码
  1. array('picname''application.validators.Myfile''on'=>'insert','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024'),  
  2.          array('picname''application.validators.Myfile''on'=>'update','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024','allowEmpty'=>true,),  

数组的第二个字段就是一个全路径,告诉rules,对于picname用的是application.validators.Myfile类。这个类其实很简单,只是简单的扩展了官方的CFileValidator类,大致如下:

PHP代码
  1. class Myfile extends CFileValidator{  
  2.     public $imageSize;  
  3.     public $wrongImageSize;  
  4.   
  5.     /** 
  6.      * @param CModel $object the object being validated 
  7.      * @param string $attribute the attribute being validated 
  8.      * @param CUploadedFile $file uploaded file passed to check against a set of rules 
  9.      * @return void 
  10.      */  
  11.     protected function validateFile($object$attribute$file){  
  12.         parent::validateFile($object,$attribute,$file);  
  13.         if($this->imageSize!=''&&strpos($this->imageSize,"x")!==false){  
  14.             list($width,$height) = @getimagesize($file->getTempName());  
  15.             $imageSize = sprintf("%sx%s",$width,$height);  
  16.             if($imageSize != $this->imageSize){  
  17.                 $message=$this->wrongImageSize!==null?$this->wrongImageSize : Yii::t('yii','The file "{file}" cannot be uploaded. Only files with these size are allowed: {imagesize}.');  
  18.                 $this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{extensions}'=>$this->imageSize));  
  19.             }  
  20.         }  
  21.     }  
  22. }  

我这种只是简单的判断是否与指定尺寸相符,如果需要检测小于指定范围的图片,那就需要多一点的判断了,也不会太难啦。主要是一个思路(感谢烂桔提供思路)

Tags: yii

xml2array 和 array2xml

这是一个简单的方法,其实是两种方法的其中一种:xml2array 和 array2xml 中的一种啦。
但因为array2xml的时候,没有办法做到更好的把attributes做到更好,因此,就折腾了一些简单的处理方法:
array2xml是hightman的方法的简版。我自己改了一些:

PHP代码
  1. function array2xml($var$type = 'root'$tag = '') {  
  2.    $ret = '';  
  3.    if (!is_int($type)) {  
  4.        if ($tag)  
  5.            return array2xml(array($tag => $var), 0, $type); else {  
  6.            $tag .= $type;  
  7.            $type = 0;  
  8.        }  
  9.    }  
  10.    $level = $type;  
  11.    $indent = str_repeat("\t"$level);  
  12.    if (!is_array($var)) {  
  13.        $ret .= $indent . '<' . $tag;  
  14.        $var = strval($var);  
  15.        if ($var == '') {  
  16.            $ret .= ' />';  
  17.        } else if (!preg_match('/[^0-9a-zA-Z@\._:\/-]/'$var)) {  
  18.            $ret .= '>' . $var . '</' . $tag . '>';  
  19.        } else {  
  20.            $ret .= "><![CDATA[{$var}]]></{$tag}>";  
  21.        }  
  22.        //                if (strpos($var, "\n") === false){  
  23.        //                $ret .= '><![CDATA[' . $var . ']]></' . $tag . '>';  
  24.        //            } else  
  25.        $ret .= "\n";  
  26.    } else if (!(is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) && !emptyempty($var)) {  
  27.        foreach ($var as $tmp)  
  28.            $ret .= array2xml($tmp$level$tag);  
  29.    } else {  
  30.        $ret .= $indent . '<' . $tag;  
  31.        if ($level == 0)  
  32.            $ret .= '';  
  33.        if (isset($var['@attributes'])) {  
  34.            foreach ($var['@attributes'as $k => $v) {  
  35.                if (!is_array($v)) {  
  36.                    $ret .= sprintf(' %s="%s"'$k$v);  
  37.                }  
  38.            }  
  39.            unset($var['@attributes']);  
  40.        }  
  41.        $ret .= ">\n";  
  42.        foreach ($var as $key => $val) {  
  43.            $ret .= array2xml($val$level + 1, $key);  
  44.        }  
  45.        $ret .= "{$indent}</{$tag}>\n";  
  46.    }  
  47.    return $ret;  

在其中强加了attributes。比较恶心的方法啦。。
然后xml2array,其实以前写过,但写的不太好,所以我这次抄的是ibm的网站上的xml2json中的代码:

PHP代码
  1. define ("DEBUG", false);  
  2. // Maximum Recursion Depth that we can allow.  
  3. define ("MAX_RECURSION_DEPTH_ALLOWED", 25);  
  4. // An empty string  
  5. define ("EMPTY_STR""");  
  6. // SimpleXMLElement object property name for attributes  
  7. define ("SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES""@attributes");  
  8. // SimpleXMLElement object name.  
  9. define ("SIMPLE_XML_ELEMENT_PHP_CLASS""SimpleXMLElement");  
  10.     /** 
  11.      * @static 
  12.      * @param $simpleXmlElementObject 
  13.      * @param int $recursionDepth 
  14.      * @return array|null|string 
  15.      */  
  16.     public static function xml2array($simpleXmlElementObject$getAttributes = false , &$recursionDepth = 0 ) {  
  17.         // Keep an eye on how deeply we are involved in recursion.  
  18.         if ($recursionDepth > MAX_RECURSION_DEPTH_ALLOWED) {  
  19.             // Fatal error. Exit now.  
  20.             return (null);  
  21.         }  
  22.         if ($recursionDepth == 0) {  
  23.             if (get_class($simpleXmlElementObject) != SIMPLE_XML_ELEMENT_PHP_CLASS) {  
  24.                 // If the external caller doesn't call this function initially  
  25.                 // with a SimpleXMLElement object, return now.  
  26.                 return (null);  
  27.             } else {  
  28.                 // Store the original SimpleXmlElementObject sent by the caller.  
  29.                 // We will need it at the very end when we return from here for good.  
  30.                 $callerProvidedSimpleXmlElementObject = $simpleXmlElementObject;  
  31.             }  
  32.         } // End of if ($recursionDepth == 0) {  
  33.         if (get_class($simpleXmlElementObject) == SIMPLE_XML_ELEMENT_PHP_CLASS) {  
  34.             // Get a copy of the simpleXmlElementObject  
  35.             $copyOfsimpleXmlElementObject = $simpleXmlElementObject;  
  36.             // Get the object variables in the SimpleXmlElement object for us to iterate.  
  37.             $simpleXmlElementObject = get_object_vars($simpleXmlElementObject);  
  38.         }  
  39.         // It needs to be an array of object variables.  
  40.         if (is_array($simpleXmlElementObject)) {  
  41.             // Initialize the result array.  
  42.             $resultArray = array();  
  43.             // Is the input array size 0? Then, we reached the rare CDATA text if any.  
  44.             if (count($simpleXmlElementObject) <= 0) {  
  45.                 // Let us return the lonely CDATA. It could even be  
  46.                 // an empty element or just filled with whitespaces.  
  47.                 return (trim(strval($copyOfsimpleXmlElementObject)));  
  48.             }  
  49.             // Let us walk through the child elements now.  
  50.             foreach ($simpleXmlElementObject as $key => $value) {  
  51.                 // When this block of code is commented, XML attributes will be  
  52.                 // added to the result array.  
  53.                 // Uncomment the following block of code if XML attributes are  
  54.                 // NOT required to be returned as part of the result array.  
  55.                 /* 
  56.           if((is_string($key)) && ($key == SIMPLE_XML_ELEMENT_OBJECT_PROPERTY_FOR_ATTRIBUTES)) { 
  57.                   continue; 
  58.                 } 
  59.                 */  
  60.                 // Let us recursively process the current element we just visited.  
  61.                 // Increase the recursion depth by one.  
  62.                 $recursionDepth++;  
  63.                 if($key == '@attributes' && $getAttributes == true){  
  64.                     foreach(self::xml2array($value,$getAttributes$recursionDepthas $k=>$v){  
  65.                         $resultArray[$k]=$v;  
  66.                     }  
  67.                 }else{  
  68.                     $resultArray[$key] = self::xml2array($value,$getAttributes$recursionDepth);  
  69.                 }  
  70.                 // Decrease the recursion depth by one.  
  71.                 $recursionDepth--;  
  72.             } // End of foreach($simpleXmlElementObject as $key=>$value) {  
  73.             if ($recursionDepth == 0) {  
  74.                 // That is it. We are heading to the exit now.  
  75.                 // Set the XML root element name as the root [top-level] key of  
  76.                 // the associative array that we are going to return to the caller of this  
  77.                 // recursive function.  
  78.                 $tempArray = $resultArray;  
  79.                 $resultArray = array();  
  80.                 $resultArray[$callerProvidedSimpleXmlElementObject->getName()] = $tempArray;  
  81.             }  
  82.             return ($resultArray);  
  83.         } else {  
  84.             // We are now looking at either the XML attribute text or  
  85.             // the text between the XML tags.  
  86.             return (trim(strval($simpleXmlElementObject)));  
  87.         } // End of else  
  88.     }  

改过其中的几行代码,否则会报错,改了哪几行我忘了。。官网地址是:http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

这里还有一个:http://www.zenme.org/?action=show&id=270,可以参考一下

Tags: xml2array

Records:3812345678