写了一个PHP的String类

PHP String 类,暂时只有encode,decode方法:

 使用方法:

$s = '中国';

$os = new String( $s );

echo $os->decode('gbk') , ' ';

echo $os->decode('gbk')->encode('md5'), ' ';

 

代码
class String extends stdClass
{
    
private $_val = '';
    
public function __construct( $str = '' )
    {
        
$this->_val = $str;
    }
    
    
public function __toString()
    {
        
return $this->_val;
    }
    
    
public function encode( $coder )
    {
        
$coder = 'encode_' . $coder;
        
ifmethod_exists$this, $coder ) )
        {
            
return $this->$coder();
        }else{
            
return $this;
        }
    }
    
    
public function decode( $coder )
    {
        
$coder = 'decode_' . $coder;
        
ifmethod_exists$this, $coder ) )
        {
            
return $this->$coder();
        }else{
            
return $this;
        }
    }
    
    
private function encode_md5()
    {
        
return new Stringmd5$this->_val ) );
    }
    
    
private function decode_gbk()
    {
        
return new Stringiconv'GBK', 'UTF-8', $this->_val ) );
    }
    
    
}


posted on 2010-04-20 15:46  还是刀哥靠谱  阅读(362)  评论(0编辑  收藏  举报

导航