Chinaunix首页 | 论坛 | 博客
  • 博客访问: 159563
  • 博文数量: 19
  • 博客积分: 470
  • 博客等级: 下士
  • 技术积分: 252
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-06 09:16
文章分类

全部博文(19)

文章存档

2013年(2)

2012年(17)

分类: Python/Ruby

2012-03-06 12:58:15

  1. 程序一:PHP截取中文字符串方法
  2. function msubstr($str, $start, $len) {
  3. $tmpstr = “”;
  4. $strlen = $start + $len;
  5. for($i = 0; $i < $strlen; $i++) {
  6. if(ord(substr($str, $i, 1)) > 0xa0) {
  7. $tmpstr .= substr($str, $i, 2);
  8. $i++;
  9. } else
  10. $tmpstr .= substr($str, $i, 1);
  11. }
  12. return $tmpstr;
  13. }


  14. 程序二:PHP截取UTF-8字符串,解决半字符问题

  15. /******************************************************************
  16. * PHP截取UTF-8字符串,解决半字符问题。
  17. * 英文、数字(半角)为1字节(8位),中文(全角)为3字节
  18. * @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串
  19. * @param $str 源字符串
  20. * $len 左边的子串的长度
  21. ****************************************************************/
  22. function utf_substr($str,$len)
  23. {
  24. for($i=0;$i<$len;$i++)
  25. {
  26. $temp_str=substr($str,0,1);
  27. if(ord($temp_str) > 127)
  28. {
  29. $i++;
  30. if($i<$len)
  31. {
  32. $new_str[]=substr($str,0,3);
  33. $str=substr($str,3);
  34. }
  35. }
  36. else
  37. {
  38. $new_str[]=substr($str,0,1);
  39. $str=substr($str,1);
  40. }
  41. }
  42. return join($new_str);
  43. }
  44. ?>

  45. php utf-8 字符串截取

  46. function cutstr($string, $length) {
  47. preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $string, $info);
  48. for($i=0; $i $wordscut .= $info[0][$i];
  49. $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
  50. if ($j > $length - 3) {
  51. return $wordscut.” …”;
  52. }
  53. }
  54. return join(, $info[0]);
  55. }
  56. $string=”242432反对感是456犯得上广泛大使馆地方7890″;
  57. for($i=0;$i {
  58. echo cutstr($string,$i)."
  59. “;
  60. }
  61. ?>

  62. 截取utf-8字符串函数

  63. 为了支持多语言,数据库里的字符串可能保存为UTF-8编码,在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象,编写如下的UTF-8字符串截取函数

  64. 关于utf-8的原理请看 UTF-8 FAQ

  65. UTF-8编码的字符可能由1~3个字节组成,具体数目可以由第一个字节判断出来。(理论上可能更长,但这里假设不超过3个字节)
  66. 第一个字节大于224的,它与它之后的2个字节一起组成一个UTF-8字符
  67. 第一个字节大于192小于224的,它与它之后的1个字节组成一个UTF-8字符
  68. 否则第一个字节本身就是一个英文字符(包括数字和一小部分标点符号)。

  69. 以前为某网站设计的代码(也是现在用在首页的长度截取的函数)

  70. //$sourcestr 是要处理的字符串
  71. //$cutlength 为截取的长度(即字数)
  72. function cut_str($sourcestr,$cutlength)
  73. {
  74. $returnstr=';
  75. $i=0;
  76. $n=0;
  77. $str_length=strlen($sourcestr);//字符串的字节数
  78. while (($n<$cutlength) and ($i<=$str_length))
  79. {
  80. $temp_str=substr($sourcestr,$i,1);
  81. $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
  82. if ($ascnum>=224) //如果ASCII位高与224,
  83. {
  84. $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
  85. $i=$i+3; //实际Byte计为3
  86. $n++; //字串长度计1
  87. }
  88. elseif ($ascnum>=192) //如果ASCII位高与192,
  89. {
  90. $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
  91. $i=$i+2; //实际Byte计为2
  92. $n++; //字串长度计1
  93. }
  94. elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,
  95. {
  96. $returnstr=$returnstr.substr($sourcestr,$i,1);
  97. $i=$i+1; //实际的Byte数仍计1个
  98. $n++; //但考虑整体美观,大写字母计成一个高位字符
  99. }
  100. else //其他情况下,包括小写字母和半角标点符号,
  101. {
  102. $returnstr=$returnstr.substr($sourcestr,$i,1);
  103. $i=$i+1; //实际的Byte数计1个
  104. $n=$n+0.5; //小写字母和半角标点等与半个高位字符宽...
  105. }
  106. }
  107. if ($str_length>$cutlength){
  108. $returnstr = $returnstr . "...

  本文转载自:[]


阅读(772) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:JS左右箭头控制图片滚动效果

给主人留下些什么吧!~~