PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/source/libs/smarty/plugins/modifier.truncate.php

https://github.com/yfg2014/ddim
PHP | 131 lines | 60 code | 18 blank | 53 comment | 12 complexity | 17b70c76c67a84ed869f34ef9d813107 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Smarty truncate modifier plugin
  9. *
  10. * Type: modifier<br>
  11. * Name: truncate<br>
  12. * Purpose: Truncate a string to a certain length if necessary,
  13. * optionally splitting in the middle of a word, and
  14. * appending the $etc string.
  15. * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
  16. * truncate (Smarty online manual)
  17. * @param string
  18. * @param integer
  19. * @param string
  20. * @param boolean
  21. * @return string
  22. */
  23. //function smarty_modifier_truncate($string, $length = 80, $etc = '...',
  24. // $break_words = false)
  25. //{
  26. // if ($length == 0)
  27. // return '';
  28. ///*
  29. // if (strlen($string) > $length) {
  30. // $length -= strlen($etc);
  31. // if (!$break_words)
  32. // $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
  33. //
  34. // return substr($string, 0, $length).$etc;
  35. // } else
  36. // return $string;
  37. //*/
  38. //
  39. // if(strlen($string) > $length) {
  40. // for($i = 0; $i < $length; $i++) {
  41. // if(ord($string[$i]) > 127) {
  42. // $wordscut .= $string[$i].$string[$i + 1];
  43. // $i++;
  44. // } else {
  45. // $wordscut .= $string[$i];
  46. // }
  47. // }
  48. // return $wordscut.$etc;
  49. // }
  50. // return $string;
  51. //
  52. //}
  53. function smarty_modifier_truncate($string, $sublen = 80, $code="UTF-8", $etc = '...', $break_words = false, $middle = false)
  54. {
  55. $start=0;
  56. if($code == 'UTF-8')
  57. {
  58. //如果有中文则减去中文的个数
  59. $cncount=cncount($string);
  60. if($cncount>($sublen/2))
  61. {
  62. $sublen=ceil($sublen/2);
  63. }
  64. else
  65. {
  66. $sublen=$sublen-$cncount;
  67. }
  68. $pa = "/[\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]/";
  69. preg_match_all($pa, $string, $t_string);
  70. if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
  71. return join('', array_slice($t_string[0], $start, $sublen));
  72. }
  73. else
  74. {
  75. $start = $start*2;
  76. $sublen = $sublen*2;
  77. $strlen = strlen($string);
  78. $tmpstr = '';
  79. for($i=0; $i<$strlen; $i++)
  80. {
  81. if($i>=$start && $i<($start+$sublen))
  82. {
  83. if(ord(substr($string, $i, 1))>129)
  84. {
  85. $tmpstr.= substr($string, $i, 2);
  86. }
  87. else
  88. {
  89. $tmpstr.= substr($string, $i, 1);
  90. }
  91. }
  92. if(ord(substr($string, $i, 1))>129) $i++;
  93. }
  94. if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
  95. return $tmpstr;
  96. }
  97. }
  98. function cncount($str)
  99. {
  100. $len=strlen($str);
  101. $cncount=0;
  102. for($i=0;$i<$len;$i++)
  103. {
  104. $temp_str=substr($str,$i,1);
  105. if(ord($temp_str) > 127)
  106. {
  107. $cncount++;
  108. }
  109. }
  110. return ceil($cncount/3);
  111. }
  112. /* vim: set expandtab: */
  113. ?>