我有一个用希腊语写的新闻网站。
在我的主页中,我必须显示每个故事的标题以及故事内容的一些字符。
我想在最后一个符合我设置的字符限制的单词中删减故事的内容。
我通过使用一些多字节安全函数并将它们组合起来实现了这一点,但它似乎在性能上非常昂贵,因为它花费了我大约 150 毫秒的时间来获取一个页面,而无需剪切字符串就可以在不到 10 毫秒的时间内加载。
所以我想知道。有没有办法对下面的函数进行性能调整并保持它们多字节的安全,或者我应该接受它们吗?
// multibyte wordcutter
function cutString( $string , $width = 20 , $cut=false ){
if( mb_strlen( $string , "UTF-8" ) == 0 ){
return $string;
}
$input = $string;
if ( mb_strlen( $string , "UTF-8" ) > $width ) {
$string = mb_wordwrap( $string , $width , "\n" , $cut );
$string = mb_substr( $string , 0 , mb_strpos( $string , "\n" , NULL , "UTF-8" ) , "UTF-8" );
if( mb_strlen( $string , "UTF-8" ) > $width ) {
$string = mb_wordwrap( $string , $width , "\n" , true );
$string = mb_substr( $string , 0 , mb_strpos( $string , "\n" , NULL , "UTF-8" ) , "UTF-8" );
}
elseif( mb_strlen( $string , "UTF-8" ) == 0 ){
$string = mb_wordwrap( $input , $width , "\n" , true );
$string = mb_substr( $string , 0 , mb_strpos( $string , "\n" , NULL , "UTF-8" ) , "UTF-8" );
}
}
return $string;
}
// multibyte safe wordwrap
function mb_wordwrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8')
{
$stringWidth = iconv_strlen($string, $charset);
$breakWidth = iconv_strlen($break, $charset);
if (strlen($string) === 0) {
return '';
} elseif ($breakWidth === null) {
throw new Zend_Text_Exception('Break string cannot be empty');
} elseif ($width === 0 && $cut) {
throw new Zend_Text_Exception('Can\'t force cut when width is zero');
}
$result = '';
$lastStart = $lastSpace = 0;
for ($current = 0; $current < $stringWidth; $current++) {
$char = mb_substr($string, $current, 1, $charset);
if ($breakWidth === 1) {
$possibleBreak = $char;
} else {
$possibleBreak = mb_substr($string, $current, $breakWidth, $charset);
}
if ($possibleBreak === $break) {
$result .= mb_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset);
$current += $breakWidth - 1;
$lastStart = $lastSpace = $current + 1;
} elseif ($char === ' ') {
if ($current - $lastStart >= $width) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
$lastStart = $current + 1;
}
$lastSpace = $current;
} elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset) . $break;
$lastStart = $lastSpace = $current;
} elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) {
$result .= mb_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break;
$lastStart = $lastSpace = $lastSpace + 1;
}
}
if ($lastStart !== $current) {
$result .= mb_substr($string, $lastStart, $current - $lastStart, $charset);
}
return $result;
}
编辑:这就是我最终使用的
// multibyte wordcutter
function cutString( $string , $width = 70 , $cut=false ){
$string = mb_substr( $string , 0 , $width , "UTF-8" );
if( mb_strlen( $string , "UTF-8" ) < $width ){
return $string;
}
$dot = mb_strripos( $string , '.' , 0 , "UTF-8" ) + 1;
$space = mb_strripos( $string , ' ' , 0 , "UTF-8" );
if( $space ){
if( ($space-$dot) < 20 ){
$string = mb_substr( $string , 0 , $dot , "UTF-8" );
}
else {
$string = mb_substr( $string , 0 , $space , "UTF-8" );
}
}
return $string;
}