我们的生活比蜜甜

人生来就不是为了被打败的,人能够被毁灭,但是不能够被打败。
    <<  分布式服务器,查看各台服务器网页内容 | 首 页 | 基于OpenCV的PHP图像人脸识别技术  >>

    2009-04-26

    中文截取,出现乱码,解决方法 - [php]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://bbayou.blogbus.com/logs/38518006.html

    1. <?php  
    2. $text = "牛逼牛逼牛逼的牛牛逼的逼哈哈哈";  
    3. $value = substr($text, 0, 18);  
    4. $value_length = strlen($value);     
    5. $value_count = 0;     
    6. for ($i = 0; $i < $value_length; $i++)     
    7. {     
    8.     if (ord($value{$i}) > 127)     
    9.     {     
    10.         $value_count++;     
    11.     }     
    12. }     
    13. if ($value_count % 2 != 0)     
    14. {     
    15.     $value = substr($text, 0, $value_length - 1);   
    16. }  
    17. echo $value."<BR>";  
    18. ?> 

    -------------------------------------------------------------------------

    function cut_str($msg,$cut_size,$charset="UTF-8",$suffix="...")
    {
    if($cut_size<=0) return $msg;
    $i=1;$han=0;$eng=0;
    while ($i <= strlen($msg)) {
      if(ord($msg[($i-1)])>127){
       $han++;
       if($charset=="UTF-8"){
        $i=$i+3;
       }else{
        $i=$i+2;
       }
      }else{
       $eng++;
       $i=$i+1;
      }
      if(($han+$eng)==$cut_size){
       if($charset=="UTF-8"){
        $cut_size = $eng + (int)$han*3;
       }else{
        $cut_size = $eng + (int)$han*2;
       }
       break;
      }
    }
    for ($i=0;$i<$cut_size;$i++) {
      $str .=$msg[$i];
    }
    return $str.$suffix;
    }


    随机文章:

    提交textarea到mysql,换行 2009-10-13
    中文全文搜索解决方案sphinx+mysql+mmseg,安装,配置,API调用 2009-08-21
    php的调试工具XHProf的安装,运用 2009-08-20
    中文分词,应用 2009-08-13
    获取文件扩展名 2009-08-13

    收藏到:Del.icio.us




    Tag:php
    引用地址:
    bbayou 发表于16:35:54 | 编辑 | 继续话题 | 转发 | 分享 0

文章分类

  • php[15]
  • JQuery[5]
  • knowledge[3]
  • life[0]
  • DB[0]
  • DB[1]
  • python[2]
  • linux[8]
  • service[6]
  • mysql[13]

Tag

  • php[21]
  • Mysql[18]
  • linux[15]
  • knowledge[5]
  • JS[4]
More..

存档

  • 2009-11[2]
  • 2009-10[3]
  • 2009-09[4]
  • 2009-08[11]
  • 2009-07[5]
  • 2009-06[4]
  • 2009-05[4]
  • 2009-04[14]
  • 2009-03[7]
  • 2009-02[1]
  • 2009-01[1]
  • 2008 [2]

搜索

最新日志

  • 我的第一个python脚本
  • python取BT种子的hashinfo
  • 123
  • 7个简易网页效果mootools插件
  • 提交textarea到mysql,换行
  • mysql 关于索引
  • 用nginx的反向代理做负载均衡
  • 2009-09-18
  • http_load 一个httpd 压力测试工具
  • mysqldumpslow分析mysql运行
全部日志>>

最新评论

  • A导:你要把我笑死了 我竟然翻到了你的博客 我的妈呀 你...
  • 123:你真可爱.
  • dongdandd:小狐狸史上最无聊的blogcn? 刚确实很无聊的点了自...
  • RSS 什么是RSS?
    用IM提醒我内容更新
    订阅到QQ邮箱
    订阅到鲜果阅读器
    订阅到Google阅读器
    订阅到抓虾阅读器
  • 《城客》第四期:创意之城
    博客大巴
    博客大巴使用指南
    博客大巴模板中心
    免费注册博客大巴
    一键博客搬家工具
    中文互动杂志城客
Copyright © 2002-2009 BlogBus.com, All Rights Reserved. 博客大巴 版权所有
博客大巴模板设计:Blue Emotionless Kid | 作者:iZ