里面有各种大家开发中常用到的种编码如uft8->gbk gbk转utf8 繁体转简体 简体转繁体 utf8转unicode gbk转拼音 Ascii转拼音 等
<?php教程 /** * utf8转gbk * @param $utfstr */ function utf8_to_gbk($utfstr) { global $UC2GBTABLE; $okstr = ''; if(empty($UC2GBTABLE)) { $filename = CODETABLEDIR.'gb-unicode.table'; $fp = fopen($filename, 'rb'); while($l = fgets($fp,15)) { $UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6)); } fclose($fp); } $okstr = ''; $ulen = strlen($utfstr); for($i=0; $i<$ulen; $i++) { $c = $utfstr[$i]; $cb = decbin(ord($utfstr[$i])); if(strlen($cb)==8) { $csize = strpos(decbin(ord($cb)),'0'); for($j = 0; $j < $csize; $j++) { $i++; $c .= $utfstr[$i]; } $c = utf8_to_unicode($c); if(isset($UC2GBTABLE[$c])) { $c = dechex($UC2GBTABLE[$c]+0x8080); $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3])); } else { $okstr .= '&#'.$c.';'; } } else { $okstr .= $c; } } $okstr = trim($okstr); return $okstr; } /** * gbk转utf8 * @param $gbstr */ function gbk_to_utf8($gbstr) { global $CODETABLE; if(empty($CODETABLE)) { $filename = CODETABLEDIR.'gb-unicode.table'; $fp = fopen($filename, 'rb'); while($l = fgets($fp,15)) { $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6); } fclose($fp); } $ret = ''; $utf8 = ''; while($gbstr) { if(ord(substr($gbstr, 0, 1)) > 0x80) { $thisW = substr($gbstr, 0, 2); $gbstr = substr($gbstr, 2, strlen($gbstr)); $utf8 = ''; @$utf8 = unicode_to_utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080])); if($utf8 != '') { for($i = 0; $i < strlen($utf8); $i += 3) $ret .= chr(substr($utf8, $i, 3)); } } else { $ret .= substr($gbstr, 0, 1); $gbstr = substr($gbstr, 1, strlen($gbstr)); } } return $ret; } /** * 繁体转简体 * @param $Text */ function big5_to_gbk($Text) { global $BIG5_DATA; if(empty($BIG5_DATA)) { $filename = CODETABLEDIR.'big5-gb.table'; $fp = fopen($filename, 'rb'); $BIG5_DATA = fread($fp, filesize($filename)); fclose($fp); } $max = strlen($Text)-1; for($i = 0; $i < $max; $i++) { $h = ord($Text[$i]); if($h >= 0x80) { $l = ord($Text[$i+1]); if($h==161 && $l==64) { $gbstr = ' '; } else { $p = ($h-160)*510+($l-1)*2; $gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1]; } $Text[$i] = $gbstr[0]; $Text[$i+1] = $gbstr[1]; $i++; } } return $Text; } /** * 简体转繁体 * @param $Text */ function gbk_to_big5($Text) { global $GB_DATA; if(empty($GB_DATA)) { $filename = CODETABLEDIR.'gb-big5.table'; $fp = fopen($filename, 'rb'); $gb = fread($fp, filesize($filename)); fclose($fp); } $max = strlen($Text)-1; for($i = 0; $i < $max; $i++) { $h = ord($Text[$i]); if($h >= 0x80) { $l = ord($Text[$i+1]); if($h==161 && $l==64) { $big = ' '; } else { $p = ($h-160)*510+($l-1)*2; $big = $GB_DATA[$p].$GB_DATA[$p+1]; } $Text[$i] = $big[0]; $Text[$i+1] = $big[1]; $i++; } } return $Text; } /** * unicode转utf8 * @param $c */ function unicode_to_utf8($c) { $str = ''; if($c < 0x80) { $str .= $c; } elseif($c < 0x800) { $str .= (0xC0 | $c >> 6); $str .= (0x80 | $c & 0x3F); } elseif($c < 0x10000) { $str .= (0xE0 | $c >> 12); $str .= (0x80 | $c >> 6 & 0x3F); $str .= (0x80 | $c & 0x3F); } elseif($c < 0x200000) { $str .= (0xF0 | $c >> 18); $str .= (0x80 | $c >> 12 & 0x3F); $str .= (0x80 | $c >> 6 & 0x3F); $str .= (0x80 | $c & 0x3F); } return $str; } /** * utf8转unicode * @param $c */ function utf8_to_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; } } /** * Ascii转拼音 * @param $asc * @param $pyarr */ function asc_to_pinyin($asc,&$pyarr) { if($asc < 128)return chr($asc); elseif(isset($pyarr[$asc]))return $pyarr[$asc]; else { foreach($pyarr as $id => $p) { if($id >= $asc)return $p; } } } /** * gbk转拼音 * @param $txt */ function gbk_to_pinyin($txt) { if(CHARSET != 'gbk') { $txt = iconv(CHARSET,'GBK',$txt); } $l = strlen($txt); $i = 0; $pyarr = array(); $py = array(); $filename = CODETABLEDIR.'gb-pinyin.table'; $fp = fopen($filename,'r'); while(!feof($fp)) { $p = explode("-",fgets($fp,32)); $pyarr[intval($p[1])] = trim($p[0]); } fclose($fp); ksort($pyarr); while($i<$l) { $tmp = ord($txt[$i]); if($tmp>=128) { $asc = abs($tmp*256+ord($txt[$i+1])-65536); $i = $i+1; } else $asc = $tmp; $py[] = asc_to_pinyin($asc,$pyarr); $i++; } return $py; }
永久链接:
转载随意!带上文章地址吧。

如何解决PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory在使用PHP开发过程中,我们经常会遇到一些文件操作的问题,其中之一就是"PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory

如何解决PHPWarning:fopen():SSLoperationfailedinfile.phponlineX在PHP编程中,我们经常使用fopen函数来打开文件或者URL,并进行相关操作。然而,在使用fopen函数时,有时候会遇到类似于Warning:fopen():SSLoperationfailedinfile.p

如何解决PHPWarning:fopen():failedtoopenstream:Permissiondenied在开发PHP程序的过程中,我们常常会遇到一些报错信息,比如PHPWarning:fopen():failedtoopenstream:Permissiondenied。这个错误通常是由于文件或目录权限不正

在进行文本处理过程中,对于不同编码格式的字符串进行转换是常见的需求。而PHP语言中提供的iconv(InternationalizationConvertion)函数可以非常方便地满足这一需求。本文将从以下几个方面详细介绍iconv函数的使用方法:iconv函数的定义和常见参数介绍实例演示:将GBK编码的字符串转换为UTF-8编码的字符串实例演示:将UTF

在Matlab中,fopen函数用于打开文件并返回文件标识符,以便后续对文件进行读取或写入操作。根据需要选择适当的权限选项来打开文件,并在操作完成后及时关闭文件。需要注意的是,打开文件后需要确保在不再需要文件时及时关闭文件,以释放系统资源。另外,如果文件打开失败或操作出错,可以通过错误处理机制进行相应的处理。

PHP中的explode函数是一种用来将字符串分割成数组的函数,它非常常用且灵活。在使用explode函数的过程中,常常会遇到一些报错和问题,本文将介绍explode函数的基本用法并提供一些解决报错的方法。一、explode函数基本用法在PHP中,explode函数的基本语法如下:explode(string$separator,string$stri

在PHP开发中,对文件的操作是非常常见的。一般情况下,我们需要进行文件的读取、写入、删除等操作。其中,文件的读取可以使用fopen函数和fread函数,文件的写入可以使用fopen函数、fwrite函数和fclose函数。本文将介绍php如何使用fopen、fwrite和fclose进行文件操作。一、fopen函数fopen函数用于打开文件,它的语法如下:r

C中的fopen()方法用于打开指定的文件。我们举个例子来理解一下问题语法FILE*fopen(filename,mode)以下是使用fopen()打开文件的有效模式:‘r’、‘w’、‘a’、‘r+’、‘w+’、‘a+’。详细信息请访问C库函数-fopen()


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
