看小说: 40行程序写成的
http://iamsese.cn/view/xiaoshuo
http://iamsese.cn/view/xiaoshuo
自己实现的PHP UNICODE 解析和反解析类
网上找到的都是 PHP到什么GBK GB2312的,但是对于 日文 俄文等等就不行了,
我做的这个代码 'iamsese.unicode.php' 终于解决了这些问题,哈哈哈
你查看源代码 看到 页面标题处
$title = IAMSESE_UNICODE::encode("{小说应用 -- начина } ");
$title .= IAMSESE_UNICODE::deUnicode($title);
写道
<?php
require_once 'woshisese.unicode.php' ;
$title = woshisese_UNICODE::encode("{小说应用 -- начина } ");
$title .= woshisese_UNICODE::deUnicode($title);
if (file_exists('./woshisese/yes.txt')) {
if ($_REQUEST['index'] != '' && is_numeric($_REQUEST['index']) && ($_REQUEST['index'] >= 1)){
$current = $_REQUEST['index'] ;
}
else
$current = $_REQUEST['index'] = 1 ;
$file = "./woshisese/ehan{$current}.txt" ;
$content = file_get_contents($file);
$array = explode("\n",$content);
//去除最后一个空字符
$array = array_slice($array,0,count($array)-1);
$content = '' ;
foreach($array as $e){
$e = woshisese_UNICODE::encode($e);
$content .= "<p>{$e}</p>";
}
}
else {
$woshisesesource = './ehan.txt' ; //
$f = fopen($woshisesesource,'r');
$filecount = 1 ;
//if ($f) die('not sourve');
while (!feof($f)) {
$str = '' ;
for ($i=0;$i<100;$i++) {
$buffer = fgets($f, 10240);
$str .= $buffer ;
}
//die($str);
//每个文件存100行
file_put_contents("./woshisese/ehan{$filecount}.txt",$str);
$filecount ++ ;
}
file_put_contents("./woshisese/yes.txt","yes");
fclose($f);
$content = "数据建立完成,请刷新当前页显示";
}
?>
<html>
<head>
<title><?php echo $title ; ?></title>
</head>
<body>
<div id='container'>
<?php echo $content ; ?>
</div>
<div>
<a href='index.php?index=<?php echo ($current-1) ;?>'>上一页</a>
<a href='index.php?index=<?php echo (1) ;?>'>第一页</a>
<a href='index.php?index=<?php echo ($current+1) ;?>'>下一页</a>
</div>
</body>
</html>
<style>
html,body {
height: 100%;
margin: 0 0 ;background-color: gray;
text-align: center ;
}
#container {
height: 80%;width: 800px;
margin: 30px auto ;
background-color: #ffc;
overflow: auto ;
}
#container p {
text-indent: 2em ;
text-align: left ;
}
</style>
<script>
</script>
require_once 'woshisese.unicode.php' ;
$title = woshisese_UNICODE::encode("{小说应用 -- начина } ");
$title .= woshisese_UNICODE::deUnicode($title);
if (file_exists('./woshisese/yes.txt')) {
if ($_REQUEST['index'] != '' && is_numeric($_REQUEST['index']) && ($_REQUEST['index'] >= 1)){
$current = $_REQUEST['index'] ;
}
else
$current = $_REQUEST['index'] = 1 ;
$file = "./woshisese/ehan{$current}.txt" ;
$content = file_get_contents($file);
$array = explode("\n",$content);
//去除最后一个空字符
$array = array_slice($array,0,count($array)-1);
$content = '' ;
foreach($array as $e){
$e = woshisese_UNICODE::encode($e);
$content .= "<p>{$e}</p>";
}
}
else {
$woshisesesource = './ehan.txt' ; //
$f = fopen($woshisesesource,'r');
$filecount = 1 ;
//if ($f) die('not sourve');
while (!feof($f)) {
$str = '' ;
for ($i=0;$i<100;$i++) {
$buffer = fgets($f, 10240);
$str .= $buffer ;
}
//die($str);
//每个文件存100行
file_put_contents("./woshisese/ehan{$filecount}.txt",$str);
$filecount ++ ;
}
file_put_contents("./woshisese/yes.txt","yes");
fclose($f);
$content = "数据建立完成,请刷新当前页显示";
}
?>
<html>
<head>
<title><?php echo $title ; ?></title>
</head>
<body>
<div id='container'>
<?php echo $content ; ?>
</div>
<div>
<a href='index.php?index=<?php echo ($current-1) ;?>'>上一页</a>
<a href='index.php?index=<?php echo (1) ;?>'>第一页</a>
<a href='index.php?index=<?php echo ($current+1) ;?>'>下一页</a>
</div>
</body>
</html>
<style>
html,body {
height: 100%;
margin: 0 0 ;background-color: gray;
text-align: center ;
}
#container {
height: 80%;width: 800px;
margin: 30px auto ;
background-color: #ffc;
overflow: auto ;
}
#container p {
text-indent: 2em ;
text-align: left ;
}
</style>
<script>
</script>
这里顺带着就解决,前后端ajax unicode编码的问题一起全部解决吧
<?php
/**
* 时间: 2009-4-9 17:19:55
* iamsese.cn 我是色色
* vb2005xu.iteye.com
*
* Unicode 编码和转换功能库
*/
//class B {
// static $name = "序列化操作--方法返回" ;
// function getB(){
// return B::$name ;
// }
// var $bname = "序列化操作--方法返回" ;
// function getC(){
// return $this->bname ;
// }
//}
class IAMSESE_UNICODE {
// function getB(){
// return B::getB();
// }
// function getC(){
// return new B() ;
// }
//
/**
* ord 方法的unicode 支持
*
* @param 单字 $c
* @return int
*/
function uniord($c) {
$h = ord($c{0});
if ($h <= 0x7F) {
return $h;
} else if ($h < 0xC2) {
return false;
} else if ($h <= 0xDF) {
return ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
} else if ($h <= 0xEF) {
return ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6
| (ord($c{2}) & 0x3F);
} else if ($h <= 0xF4) {
return ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12
| (ord($c{2}) & 0x3F) << 6
| (ord($c{3}) & 0x3F);
} else {
return false;
}
}
/**
* 将指定编码的字符串分解成数组
*
* @param STRING $string
* @param STRING $encode
* @return ARRAY
*/
function mbStringToArray ($string,$encode="UTF-8") {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,0,1,$encode);
$string = mb_substr($string,1,$strlen,$encode);
$strlen = mb_strlen($string);
}
return $array;
}
/**
* 仅支持PHP5 -- 同上,是icov实现
*/
// function iconvStringToArray ($string,$encode="UTF-8") {
// $strlen = iconv_strlen($string,$encode);
// while ($strlen) {
// $array[] = iconv_substr($string,0,1,$encode);
// $string = iconv_substr($string,1,$strlen,$encode);
// $strlen = iconv_strlen($string,$encode);
// }
// return $array;
// }
/**
* Unicode 编码表示
*
* @param 单字 $unichar
* @return String
*/
function unicharCodeAt($unichar){
return "&#" . IAMSESE_UNICODE::uniord($unichar) . ';' ;
}
/**
* chr 函数的unicode实现
*
* @param mixed[整形数组或者整数] $codes
* @return String
*/
function uchr ($codes) {
if (is_scalar($codes))
$codes= func_get_args();
$str= '';
foreach ($codes as $code)
$str.= html_entity_decode('&#'.$code.';',ENT_NOQUOTES,'UTF-8');
return $str;
}
/*
* 同上 基于mb实现
*/
// function unichr($codes) {
// if (is_scalar($codes))
// $codes= func_get_args();
// $str= '';
// foreach ($codes as $code)
// $str.= mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
// return $str;
// }
/**
* 将unicode编码的字符串解析成正常的数据
*
* @param String $str
* @return String
*/
function deUnicode($str){
$arr = explode (';',str_ireplace('&#','',$str) );
//去除最后一个空字符
$arr = array_slice($arr,0,count($arr)-1);
return IAMSESE_UNICODE::uchr($arr);
}
/**
* 将字符串解析成unicode编码的字符串
*
* @param String $str
* @return String
*/
function encode($str){
$arr = IAMSESE_UNICODE::mbStringToArray($str);
$str = '' ;
foreach($arr as $a){
$str .= IAMSESE_UNICODE::unicharCodeAt($a) ;
}
return $str ;
}
/**
* 输出Unicode字符表
*
*/
function echoUnicodeTable(){
for ($i=0;$i<4096;$i++){
echo '<br/>' . dechex($i). ' ********** ';
for ($j=0;$j<16;$j++){
$ch = intval($i * 16 + $j);
$ch = IAMSESE_UNICODE::uchr($ch);
echo $ch . ' -- ';
}
}
}
/**
* 输出Ascii字符表
*
*/
function echoAsciiTable(){
for($i=0;$i<16;$i++){
echo '<br/>' . ($i). ' ********** ';
for ($j=0;$j<16;$j++){
$num = intval($i * 16 + $j);
$ch = IAMSESE_UNICODE::uchr($num);
echo "{$ch} --- ";
}
}
}
function test(){
$test1 ='我是色色[iamsese.cn] начинается уже в марте bbs.cn #$%^&*()_+!@QADD?><.,,m';
$test1 = IAMSESE_UNICODE::encode($test1);
echo $test1 ;
echo "\n<br/>\n";
$test1 = IAMSESE_UNICODE::deUnicode($test1);
echo $test1 ;
}
}
//IAMSESE_UNICODE::test();
//IAMSESE_UNICODE::echoUnicodeTable();
//IAMSESE_UNICODE::echoAsciiTable();
?>
前端js代码
jQuery.iamsese = { version : '过客阵营 -- 我是色色 !', // ++++++++Unicode 编码转换++++++++ // toUnicode : function(s) { var ctpl = '&#{c};' var len = s.length; var rs = ""; for (var i = 0; i < len; i++) rs += ctpl.replace('{c}', s.charCodeAt(i)); return rs; }, deUnicode : function(s) { /** * 直接用str.replace("apples","oranges") 只会替换第一个匹配的 使用 /g 替换所有匹配的字符, /i * 忽略大小写 */ var k = s.replace(/&#/gi, "").split(";"); var rs = ""; for (i = 0; i < k.length; i++) rs += String.fromCharCode(k[i]); return rs; }