Rumah >pembangunan bahagian belakang >tutorial php > php ASCII 字符变换 (汉字及特殊字符) 宽字符 (uft8/unicode)

php ASCII 字符变换 (汉字及特殊字符) 宽字符 (uft8/unicode)

WBOY
WBOYasal
2016-06-13 13:02:431000semak imbas

php ASCII 字符转换 (汉字及特殊字符) 宽字符 (uft8/unicode)

?

有点像现在流行的输入法中的特殊字符。

?


1. 英文字母或者数字转ASCII 效果如上

?

class:

?

<?
class ascii
{
    var $asciiText;
    
    function ascii($text,$style)
    {
        switch($style)
        {
           case 'graffiti': include "graffiti.font"; break;
           case 'banner': include 'banner.font'; break;
           default: include 'dos.font'; break;
        }

        $text=strtolower($text);

        for($c=0;$c<strlen($text);$c++) $symbol[$c]=explode("\n",$arr[$text[$c]]);

        for($c=0;$c<sizeof($symbol[0]);$c++):
            for($k=0;$k<strlen($text);$k++) $output.=" ".str_replace("\r","",$symbol[$k][$c]);
            $output.="\n";
        endfor;
        
        $this->asciiText=$output;
    }
}
?

实例:

?

<PRE>
<?

include_once("ascii.class.php");        // includes the class's file
$text=new ascii("justcoding","doc");          // first parameter is text that will be converted, second is the font style
print $text->asciiText;                 // asciiText is variable of converted text

?>
?

可以通过css控制字符大小。具体见附件。

?

2. 汉字转ASCII 效果为 “星期天 ” 就是 “星期天”

?

function encode($c)
{
    $len = strlen($c);
    $a = 0;
    while ($a < $len)
    {
        $ud = 0;
        if (ord($c{$a}) >=0 && ord($c{$a})<=127)
        {
            $ud = ord($c{$a});
            $a += 1;
        }
        else if (ord($c{$a}) >=192 && ord($c{$a})<=223)
        {
            $ud = (ord($c{$a})-192)*64 + (ord($c{$a+1})-128);
            $a += 2;
        }
        else if (ord($c{$a}) >=224 && ord($c{$a})<=239)
        {
            $ud = (ord($c{$a})-224)*4096 + (ord($c{$a+1})-128)*64 + (ord($c{$a+2})-128);
            $a += 3;
        }
        else if (ord($c{$a}) >=240 && ord($c{$a})<=247)
        {
            $ud = (ord($c{$a})-240)*262144 + (ord($c{$a+1})-128)*4096 + (ord($c{$a+2})-128)*64 + (ord($c{$a+3})-128);
            $a += 4;
        }
        else if (ord($c{$a}) >=248 && ord($c{$a})<=251)
        {
            $ud = (ord($c{$a})-248)*16777216 + (ord($c{$a+1})-128)*262144 + (ord($c{$a+2})-128)*4096 + (ord($c{$a+3})-128)*64 + (ord($c{$a+4})-128);
            $a += 5;
        }
        else if (ord($c{$a}) >=252 && ord($c{$a})<=253)
        {
            $ud = (ord($c{$a})-252)*1073741824 + (ord($c{$a+1})-128)*16777216 + (ord($c{$a+2})-128)*262144 + (ord($c{$a+3})-128)*4096 + (ord($c{$a+4})-128)*64 + (ord($c{$a+5})-128);
            $a += 6;
        }
        else if (ord($c{$a}) >=254 && ord($c{$a})<=255)
        { //error
            $ud = false;
        }
        $scill .= "&#$ud;";
    }
    return $scill;
}

echo encode('日'); // &#26085; 浏览器默认会解读,所以要看源代码

?

3. utf-8 unicode 互转

?

<?
// utf8 - unicode
function utf8_unicode($c) {
   switch(strlen($c)) {
     case 1:
       return ord($c);
     case 2:
       $n = (ord($c[0]) & 0x3f) << 6;
       $n += ord($c[1]) & 0x3f;
       return $n;
     case 3:
       $n = (ord($c[0]) & 0x1f) << 12;
       $n += (ord($c[1]) & 0x3f) << 6;
       $n += ord($c[2]) & 0x3f;
       return $n;
     case 4:
       $n = (ord($c[0]) & 0x0f) << 18;
       $n += (ord($c[1]) & 0x3f) << 12;
       $n += (ord($c[2]) & 0x3f) << 6;
       $n += ord($c[3]) & 0x3f;
       return $n;
   }
}

echo utf8_unicode('以');
?>
?
<?
// unicode - utf8
function u2utf8($c) {  
	$str="";  
	if ($c < 0x80) $str.=$c;  
	else if ($c < 0x800) {  
		$str.=chr(0xC0 | $c>>6);  
		$str.=chr(0x80 | $c & 0x3F);  
	} else if ($c < 0x10000) {  
		$str.=chr(0xE0 | $c>>12);  
		$str.=chr(0x80 | $c>>6 & 0x3F);  
		$str.=chr(0x80 | $c & 0x3F);  
	} else if ($c < 0x200000) {  
		$str.=chr(0xF0 | $c>>18);  
		$str.=chr(0x80 | $c>>12 & 0x3F);  
		$str.=chr(0x80 | $c>>6 & 0x3F);  
		$str.=chr(0x80 | $c & 0x3F);  
	}
	return $str;  
}

echo u2utf8('20197');
?>
?

?

?

?

?

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya: PHP版本取舍之痛 Artikel seterusnya: FCKeditor的PHP配备