Issue 1129 using "mb_strimwidth()" function on cut_str() function.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@9991 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
ngleader 2011-12-26 04:28:11 +00:00
parent 23115d3b38
commit be55b73312

View file

@ -356,10 +356,17 @@
* @param tail: tail to put in the end of the string after trimming * @param tail: tail to put in the end of the string after trimming
* @return string * @return string
**/ **/
function cut_str($string,$cut_size=0,$tail = '...') { function cut_str($string, $cut_size = 0, $tail = '...')
{
if($cut_size < 1 || !$string) return $string; if($cut_size < 1 || !$string) return $string;
$chars = Array(12, 4, 3, 5, 7, 7, 11, 8, 4, 5, 5, 6, 6, 4, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 4, 8, 6, 8, 6, 10, 8, 8, 9, 8, 8, 7, 9, 8, 3, 6, 7, 7, 11, 8, 9, 8, 9, 8, 8, 7, 8, 8, 10, 8, 8, 8, 6, 11, 6, 6, 6, 4, 7, 7, 7, 7, 7, 3, 7, 7, 3, 3, 6, 3, 9, 7, 7, 7, 7, 4, 7, 3, 7, 6, 10, 6, 6, 7, 6, 6, 6, 9); if($GLOBALS['use_mb_strimwidth'] || function_exists('mb_strimwidth'))
{
$GLOBALS['use_mb_strimwidth'] = TRUE;
return mb_strimwidth($string, 0, $cut_size + 4, $tail, 'utf-8');
}
$chars = array(12, 4, 3, 5, 7, 7, 11, 8, 4, 5, 5, 6, 6, 4, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 4, 8, 6, 8, 6, 10, 8, 8, 9, 8, 8, 7, 9, 8, 3, 6, 7, 7, 11, 8, 9, 8, 9, 8, 8, 7, 8, 8, 10, 8, 8, 8, 6, 11, 6, 6, 6, 4, 7, 7, 7, 7, 7, 3, 7, 7, 3, 3, 6, 3, 9, 7, 7, 7, 7, 4, 7, 3, 7, 6, 10, 6, 6, 7, 6, 6, 6, 9);
$max_width = $cut_size*$chars[0]/2; $max_width = $cut_size*$chars[0]/2;
$char_width = 0; $char_width = 0;
@ -370,21 +377,29 @@
while($idx < $string_length && $char_count < $cut_size && $char_width <= $max_width) { while($idx < $string_length && $char_count < $cut_size && $char_width <= $max_width) {
$c = ord(substr($string, $idx,1)); $c = ord(substr($string, $idx,1));
$char_count++; $char_count++;
if($c<128) { if($c < 128)
{
$char_width += (int)$chars[$c-32]; $char_width += (int)$chars[$c-32];
$idx++; $idx++;
} }
else if (191<$c && $c < 224) { else if(191 < $c && $c < 224)
{
$char_width += $chars[4]; $char_width += $chars[4];
$idx += 2; $idx += 2;
} }
else { else
{
$char_width += $chars[0]; $char_width += $chars[0];
$idx += 3; $idx += 3;
} }
} }
$output = substr($string, 0, $idx); $output = substr($string, 0, $idx);
if(strlen($output)<$string_length) $output .= $tail; if(strlen($output) < $string_length)
{
$output .= $tail;
}
return $output; return $output;
} }
@ -977,4 +992,3 @@ HTMLHEADER;
echo '<script type="text/javascript">'.$reloadScript.'</script>'; echo '<script type="text/javascript">'.$reloadScript.'</script>';
} }
?>