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,37 +356,52 @@
* @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'))
$max_width = $cut_size*$chars[0]/2; {
$char_width = 0; $GLOBALS['use_mb_strimwidth'] = TRUE;
return mb_strimwidth($string, 0, $cut_size + 4, $tail, 'utf-8');
}
$string_length = strlen($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);
$char_count = 0; $max_width = $cut_size*$chars[0]/2;
$char_width = 0;
$idx = 0; $string_length = strlen($string);
while($idx < $string_length && $char_count < $cut_size && $char_width <= $max_width) { $char_count = 0;
$c = ord(substr($string, $idx,1));
$char_count++; $idx = 0;
if($c<128) { while($idx < $string_length && $char_count < $cut_size && $char_width <= $max_width) {
$char_width += (int)$chars[$c-32]; $c = ord(substr($string, $idx,1));
$idx++; $char_count++;
} if($c < 128)
else if (191<$c && $c < 224) { {
$char_width += $chars[4]; $char_width += (int)$chars[$c-32];
$idx += 2; $idx++;
} }
else { else if(191 < $c && $c < 224)
$char_width += $chars[0]; {
$idx += 3; $char_width += $chars[4];
} $idx += 2;
} }
$output = substr($string,0,$idx); else
if(strlen($output)<$string_length) $output .= $tail; {
return $output; $char_width += $chars[0];
} $idx += 3;
}
}
$output = substr($string, 0, $idx);
if(strlen($output) < $string_length)
{
$output .= $tail;
}
return $output;
}
function zgap() { function zgap() {
$time_zone = $GLOBALS['_time_zone']; $time_zone = $GLOBALS['_time_zone'];
@ -963,18 +978,17 @@ HTMLHEADER;
function alertScript($msg) function alertScript($msg)
{ {
if(!$msg) return; if(!$msg) return;
echo '<script type="text/javascript">alert("'.$msg.'");</script>'; echo '<script type="text/javascript">alert("'.$msg.'");</script>';
} }
function closePopupScript() function closePopupScript()
{ {
echo '<script type="text/javascript">window.close();</script>'; echo '<script type="text/javascript">window.close();</script>';
} }
function reload($isOpener = false) function reload($isOpener = false)
{ {
$reloadScript = $isOpener ? 'window.opener.location.reload()' : 'document.location.reload()'; $reloadScript = $isOpener ? 'window.opener.location.reload()' : 'document.location.reload()';
echo '<script type="text/javascript">'.$reloadScript.'</script>'; echo '<script type="text/javascript">'.$reloadScript.'</script>';
} }
?>