0

我有一个用希腊语写的新闻网站。

在我的主页中,我必须显示每个故事的标题以及故事内容的一些字符。

我想在最后一个符合我设置的字符限制的单词中删减故事的内容。

我通过使用一些多字节安全函数并将它们组合起来实现了这一点,但它似乎在性能上非常昂贵,因为它花费了我大约 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;
    }
4

2 回答 2

1
  1. 使用真实的测试用例和 xdebug 来检查性能。
  2. 我用这段代码测试过:

// 测试页面

<?php

    $strSmall = "ये एक हिन्दी वाक्य है, इसमे बहुत सारे शब्द हैं |";
    $strBig = implode(".",array_fill(0,100,$strSmall));  // Create a big string

    $tStart = microtime(true);
    echo cutString($strBig);
    $tEnd = microtime(true);

    echo "\nTime taken:" , ($tEnd - $tStart) , " s";

    // 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;
}

?>
  1. 花了大约 200 毫秒。

    Performance Test ये एक हिन्दी वाक्य Time taken:0.23847889900208 s

  2. This is xdebug profiling result, you can see that iconv_strlen and mb_substr are eating the time :

enter image description here

. Instead of so many function calls to make the code easy, try to minimize function calls, and write some own loops.

于 2012-12-06T08:26:45.573 回答
1

Current implementation is overcomplicated.

If I understood right then the better strategy would be:

  1. Cut the string by the length
  2. Iterate over characters from the end up to the first space
  3. Break and return the result

It should improve the performance significantly.

于 2012-12-06T09:00:49.957 回答