Home  >  Article  >  Backend Development  >  一个PHP的String类代码_PHP

一个PHP的String类代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:19:54801browse
使用方法:
复制代码 代码如下:
$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;
if( method_exists( $this, $coder ) )
{
return $this->$coder();
}else{
return $this;
}
}
public function decode( $coder )
{
$coder ='decode_' . $coder;
if( method_exists( $this, $coder ) )
{
return $this->$coder();
}else{
return $this;
}
}
private function encode_md5()
{
return new String( md5( $this->_val ) );
}
private function decode_gbk()
{
return new String( iconv('GBK','UTF-8', $this->_val ) );
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn