Home  >  Article  >  Backend Development  >  A PHP String class code_PHP tutorial

A PHP String class code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:36734browse

Usage:

Copy code The code is as follows:

$s ='China';
$os = new String( $s );
echo $os->decode('gbk') ,'';
echo $os->decode('gbk')->encode( 'md5'),'';

Code
Copy code The code is as follows:

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 ) );
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321738.htmlTechArticleUsage: Copy the code as follows: $s ='China'; $os = new String( $s ) ; echo $os-decode('gbk') ,''; echo $os-decode('gbk')-encode('md5'),''; Code copy code is as follows...
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