1
0
Fork 0
php-coding/14.work1.php

44 lines
1004 B
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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();