44 lines
1004 B
PHP
44 lines
1004 B
PHP
<?php
|
||
|
||
class codepage_change
|
||
{
|
||
private string $str;
|
||
private string $org_codepage;
|
||
private string $tar_codepage;
|
||
|
||
/**
|
||
* @param string $tar_codepage
|
||
* @param string $org_codepage
|
||
*/
|
||
public function __construct(string $org_codepage, string $tar_codepage = 'UTF-8') //必须提供原编码,由于mb_detect_encoding的检测并不准确
|
||
{
|
||
$this->org_codepage = $org_codepage;
|
||
$this->tar_codepage = $tar_codepage;
|
||
}
|
||
|
||
public function getStr(): string
|
||
{
|
||
return $this->doChange($this->str);
|
||
}
|
||
|
||
public function setStr(string $str): void
|
||
{
|
||
$this->str = $str;
|
||
}
|
||
|
||
protected function doChange($str): string
|
||
{
|
||
return iconv($this->org_codepage, $this->tar_codepage, $str);
|
||
}
|
||
|
||
}
|
||
|
||
$strr = '你好世界';
|
||
echo $strr . '<br>';
|
||
$strr2 = mb_convert_encoding($strr, 'gb2312', 'UTF-8');
|
||
|
||
$codepage_change = new codepage_change('gb2312');
|
||
$codepage_change->setStr($strr2);
|
||
echo $codepage_change->getStr();
|
||
|