0

カスタムテンプレートシステムを作成していて、テンプレートにが含まれている場所にリンクを表示したいとします{{link:http://example.com/something}}click here{{link}}

これを解析するための最適な方法は何でしょうか?

私が考えることができるいくつかのアプローチ:を使用した単純な文字列操作substrpreg_replace逆置換などで使用します。

4

1 に答える 1

3

私自身のパーサーには、「strpos/substr」とpreg_replaceを組み合わせて使用​​しています。なんで?まあ、そのすべては数字です。

解析する文字列に{{link}}タグのみが含まれ、Start-&Endnoiceがない場合、preg_replaceの方が高速です。

STARTNOICE{{link:http://example.com/something}}click here{{link}}ENDNOICE

ただし、Endnoiceに含まれる文字が多いほど、substr / strposと比較してpreg_replaceが遅くなります:)Endnoiceに含まれる文字が100文字しかないため、substr/strposはpreg_matchよりも高速になります。

これを証明する最良の方法は、原因のコードを使用することです。これがテストスクリプトです。substrの時間の合計をpreg_matchと比較します

substrははるかに多くのコードを必要としますが、より効率的です:)

グリーツ、JB

psいくつかの結果:

Startnoice = Endnoice = 100, /w 100 loops
$substrTime:
0.004448 Seconds
$pregTime:
0.004752 Seconds

Startnoice = Endnoice = 1000, /w 100 loops
$substrTime:
0.005598 Seconds
$pregTime:
0.023844 Seconds  << waaaay slower

Startnoice = Endnoice = 10000, /w 100 loops
$substrTime:
0.009028 Seconds
$pregTime:
0.278836 Seconds  << Dont use preg_replace

<?PHP
$startNoice = 1;
$endNoice   = 100;

for( $jb = 0; $jb < 100; $jb++ )
{
    $string = randomgenerator( $startNoice ) . '{{link:http://example.com/something}}click here{{link}}.' . randomgenerator( $endNoice );

/**************/
// Strpos / substr
/**************/

/* Start the timer */
    $Start = '{{link:';
    $StartEnd = '}}';
    $Stop = '{{link}}';

    $StartLen = strlen( $Start );
    $StartEndLen = strlen( $StartEnd );
    $StopLen = strlen( $Stop );

    $time_start = microtime_float();
    $strpos['Start']    = strpos( $string, $Start );

    $start = substr( $string, 0, $strpos['Start']);
    $end = substr( $string, $strpos['Start'] );

    $strpos['StartEnd'] = strpos( $end, $StartEnd, $StartLen );
    $strpos['Stop']     = strpos( $end, $Stop, $StartLen );

    $url  = substr( $end, $strpos['Start'] + $StartLen, ($strpos['StartEnd'] - $strpos['Start'] - $StartLen)  );
    $text = substr( $end, $strpos['StartEnd'] + $StartEndLen, ($strpos['Stop'] - $strpos['StartEnd'] - $StartEndLen) );

// Replace the link
    $NewString1 = $start . '<a href="' . $url . '">' . $text . '</a>' . substr( $end, $strpos['Stop'] + $StopLen );
/* Stop the tumer */
    $time_end = microtime_float();
    $substrTime[] = $time_end - $time_start;


/**************/
// Preg_match
/**************/
    $time_start = microtime_float();
    $NewString2 = preg_replace('/{{link:(.*)}}(.*){{link}}/', '<a href="$1">$2</a>', $string);
    $time_end = microtime_float();
    $pregTime[] = $time_end - $time_start;
}

echo '$substrTime: . <br />';
echo round( array_sum( $substrTime ), 6) . ' Seconds';
echo '<br />';
echo '$pregTime: . <br />';
echo round( array_sum( $pregTime ), 6) . ' Seconds';

function randomgenerator( $length )
{
    $string = 'abcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*()_+=-{}][|\::",./';
    $r = '';
    for( $i = 0; $i < $length; $i ++ )
    {
        $r .= $string[ rand(0, strlen( $string ) -1 ) ];
    }
    return $r;
}

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>
于 2012-12-03T14:16:53.143 に答える