[置顶] 网站unicode字符乱码问题一扫空解决方案 -- 带demo 简单的小说应用
来源:互联网 发布:人工智能能否代替老师 编辑:程序博客网 时间:2024/06/07 02:51
看小说: 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;}
0 0
- [置顶] 网站unicode字符乱码问题一扫空解决方案 -- 带demo 简单的小说应用
- 字符乱码问题的解决方案
- 简单的小说网站爬虫--爬小说
- 巧用GetShortPathName解决带Unicode字符路径在非Unicode程序里的无法访问的问题
- ReadString在UNICODE下中文乱码的解决办法(Char型字符和Unicode字符的转换问题)
- ReadString在UNICODE下中文乱码的解决办法(Char型字符和Unicode字符的转换问题)
- 关于URL传递日文字符乱码问题的解决方案
- Mysql 的字符编码机制、中文乱码问题及解决方案
- 解决Unicode的乱码问题
- fstream类读取UTF-8、Unicode和ANSI文本文档乱码问题的解决方案
- fstream类读取UTF-8、Unicode和ANSI文本文档乱码问题的解决方案
- fstream类读取UTF-8、Unicode和ANSI文本文档乱码问题的解决方案
- 解决使用Pyodbc向MSSQL插入unicode字符乱码的问题
- unicode和多字节字符中CString转string方法,可解决中文乱码的问题
- 简单的字符乱码处理
- 文件读写字符乱码的解决方案
- Unity3D脚本中文字符乱码的解决方案
- Unity3D脚本中文字符乱码的解决方案
- [置顶] FleaPHP中FLEA_View_Simple的bug 和 本人的修正版本
- 高效Web开发的10个jQuery代码片段
- java 百文买百鸡 鸡兔同笼 回文数 关键代码
- [置顶] Office Web化的一种解决方案
- 点击UITableViewcell 弹出AlertController 出现延迟问题
- [置顶] 网站unicode字符乱码问题一扫空解决方案 -- 带demo 简单的小说应用
- loadrunner场景对性能测试策略的映射
- [置顶] QUI继续之 -- 添加JS模板引擎功能, 征集LOGO
- 使用NSArray时,到底用strong修饰还是copy修饰
- 数据结构之关键路径
- github 建设网站 自定义绑定域名
- [置顶] ZendFramework1.0 PDF开发中-Nesting level too deep的解决
- [置顶] JAVA元数据编程零接触 -- 实现简单的MVC跳转控制雏形
- [置顶] 两步轻松搞定 Apache 本地虚拟主机设置